当前位置:网站首页>How to install postgresql and configure remote access in ubuntu environment
How to install postgresql and configure remote access in ubuntu environment
2022-08-04 14:04:00 【theskylife】
1.更新ubuntu包
sudo apt-get update
sudo apt-get upgrade
2.安装官方源
# 创建文件存储库配置
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# 导入存储库签名密钥
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# 更新包列表
sudo apt-get update
# 安装最新版本的PostgreSQL
sudo apt-get -y install postgresql
以上命令成功运行后,证明postgresql已经正常安装,It will start automatically after the installation is completepostgresql的服务.It can be viewed through the following informationpostgresql的信息
# 查看postgresql版本
sudo -u postgres psql -c "SELECT version();"
# 查看postgresql服务是否启动
service postgresql status
3.进行数据库配置
3.1 创建用户账号密码
在ubuntu上PostgreSQL数据库以后,A computer user is automatically createdpostgre,The following operations are based on this account.
运行以下命令,使用postgre用户登录postgresql
# 切换用户
sudo su postgres
# 进入交互界面
psql
进行postgresqlafter the interactive interface,User creation can be done by running the following information
-- Create an account without a password-root
create role root;
-- Create an account with a password-root
create user root with 'root123';
-- 显示所有用户,并查看是否创建成功
\du
-- 更改postgresql密码,如修改root账号的密码
alter user root with password 'root123#';
3.2 创建数据库
-- 创建数据库(test)
create database test;
-- 删除数据库(test)
drop database test;
-- 查询所有数据库
\l
-- 切换数据库test
\c test
3.3 创建schema
-- 创建schema(ods),以下写法均可
create schema ods;
create schema if not exists ods;
-- 为某个用户(root2)创建market
create schema market authorization roo2;
-- 查看当前数据库下的所有schema
select * from information_schema.schemata;
-- 删除schema(ods)
drop schema if exists ods;
3.4 用户权限授权
After creating the user password,Permissions can be granted to users with the following commands
-- 给用户root赋予超级用户权限
alter user root with superuser;
-- 给用户root2Grant ordinary permissions
alter user root2 with privileges;
-- Give all permissions to the database to the userroot
grant all privileges on database test to root;
-- 把某schema下(ods)的所有表(test用户下)All permissions are given to the userroot2
grant all on all tables in schema ods to root2;
-- put a table(test_table)All permissions are given to the userroot2
grant all on test_table to root2;
-- put a table(test_table)The query permission is given to the userroot2
grant select on table test_table to root2;
-- 撤销用户root2的某个数据库(test)权限;
revoke all on database test from root2;
-- 撤销用户root2某个schema(ods)Modifications to all tables below(update)权限;
revoke update on all tables in schema ods from root2;
-- 撤销用户root2某张表(test_table)的所有权限
revoke privileges on test_table from root2;
-- put a table(test_table)given by the ownerroot2
alter table test_table owner to root2;
-- 允许用户(root)登录
ALTER ROLE root WITH LOGIN;
-- 禁止用户(root)登录
ALTER ROLE root WITH NOLOGIN;
3.5 配置远程访问
由于postgresqlA username is added by default during installationpostgres的ubuntu系统用户,So you need to delete it with another account firstpostgres用户的密码,Then configure your own password.
# 删除postgres用户的密码
sudo passwd -d postgres
# 设置postgres用户的密码
sudo -u postgres passwd
After configuring the password,编辑/etc/postgresq/12/main/postgresql.conf文件
sudo vim /etc/postgresql/12/main/postgresql.conf
in the above configuration filepostgresql.conf中添加以下内容:
listen_addresses = '*'
password_encryption = scram-sha-256
运行以下命令,编辑/etc/postgresq/12/main/pg_hba.conf文件
sudo vim /etc/postgresql/14/main/pg_hba.conf
在pg_hba.conf文件末尾追加以下内容:
host all all 0.0.0.0/0 scram-sha-256
添加完成后,保存退出,然后重启postgresql 服务.
sudo service postgresql restart
3.6使用Navicat链接
Use the information obtained above,成功链接postgresql数据库.
3.7 卸载Navicat
# 移除postgresql
sudo apt-get --purge remove postgresql\*
# 移除配置信息
sudo rm -r /etc/postgresql/
sudo rm -r /etc/postgresql-common/
sudo rm -r /var/lib/postgresql/
sudo userdel -r postgres
sudo groupdel postgres
边栏推荐
猜你喜欢
随机推荐
大势所趋之下的nft拍卖,未来艺术品的新赋能
idea永久激活教程(新版)
k8s上安装mysql
FreeConfig.h文件
华为手机切换屏幕效果_华为p40页面切换效果怎么换
七夕当然要学会SQL优化好早点下班去找对象
第六届未来网络发展大会,即将开幕!
2546 饭卡(01背包,挺好的)
ACL 2022 | 社会科学理论驱动的言论建模
Convolutional Neural Network Basics
C# 动态加载卸载 DLL
Chinese valentine's day, of course, to learn SQL optimization better leave work early to find objects
Execution failed for task ‘:xxx:generateReleaseRFile‘.
SLAM 04.视觉里程计-1-相机模型
开放麒麟 openKylin 版本规划敲定:10 月发布 0.9 版并开启公测,12 月发布 1.0 版
MySQL【触发器】
【毕设选题推荐】机器人工程专业毕设选题推荐
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
LeetCode 1403 Minimum subsequence in non-increasing order [greedy] HERODING's LeetCode road
封装、继承、多态的联合使用实现不同等级学生分数信息的统计









