参考:https://computingforgeeks.com/install-postgresql-12-on-ubuntu/
1. 安装
代码语言:javascript复制sudo apt update
sudo apt -y install vim bash-completion wget
sudo apt -y upgrade
sudo reboot
代码语言:javascript复制curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" |sudo tee /etc/apt/sources.list.d/pgdg.list
代码语言:javascript复制sudo apt update
sudo apt -y install postgresql-12 postgresql-client-12
运行查看:systemctl status postgresql.service
postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Wed 2022-05-18 15:50:22 EAT; 15min ago
Main PID: 4317 (code=exited, status=0/SUCCESS)
CPU: 1ms
Mei 18 15:50:22 ubuntu22 systemd[1]: Starting PostgreSQL RDBMS...
Mei 18 15:50:22 ubuntu22 systemd[1]: Finished PostgreSQL RDBMS.
2. 添加用户、创建数据库
切换超管postgres 用户 sudo su - postgres
给postgres密码:psql -c "alter user postgres with password 'StrongAdminPassw0rd'"
进入命令行:psql
,创建数据库、创建用户、用户和库授权
postgres=# CREATE DATABASE mytestdb;
CREATE DATABASE
postgres=# CREATE USER mytestuser WITH ENCRYPTED PASSWORD 'MyStr0ngP@SS';
CREATE ROLE
postgres=# GRANT ALL PRIVILEGES ON DATABASE mytestdb to mytestuser;
GRANT
其他命令:列出所有库l
,退出q
3. 开启远程连接
文件位置: /etc/postgresql/12/main/postgresql.conf
# Listen on all interfaces
listen_addresses = '*'
# Listen on specified private IP address
listen_addresses = '192.168.10.11'
修改g_hba.conf
文件
在ipv4链接设置中添加一行.
#配置192.168.88.0网段的所有主机均可访问,24为子网掩码
host all all 192.168.88.0/24 md5
#测试环境不要求严格权限是可像如下配置
host all all 0.0.0.0/0 trust
重启数据库 sudo systemctl restart postgresql
4. 开启防火墙
查看状态:sudo ufw status
允许指定ip:sudo ufw allow from 192.168.1.1
或者开放端口:sudo ufw allow 2021
重启:sudo ufw reload