在Ubuntu上安装postresql客户端
如果我们只需要连接到远程POSTRESQL服务器,则只需在本地Ubuntu主机上安装PostgreSQL客户端。
$ sudo apt install postgresql-client
安装后,我们可以使用“psql命令”连接到远程POSTRESQL服务器。
$ psql -h postresql-ubuntu -U postgres psql (10.2 (Ubuntu 10.2-1)) SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off) Type "help" for help.
在Ubuntu上安装postresql服务器
在本节中,我们将在Ubuntu 18.04 Linux上安装PostgreSQL Server。
$ sudo apt install postgresql
PostreSQL安装完成后,通过检查端口号“5432”上的侦听套接字,确认它已按预期启动并运行:
$ ss -nlt State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 5 127.0.0.1:631 0.0.0.0:* LISTEN 0 128 127.0.0.1:5432 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 5 [::1]:631 [::]:*
PostgreSQL server将在重新启动后启动。要操作此默认行为,您可以通过以下方式禁用或者启用系统重新启动后启动PostreSQL:
$ sudo systemctl disable postgresql OR $ sudo systemctl enable postgresql
默认情况下,PostgreSQL server将仅在本地回送接口“127.0.0.1”上侦听。如果需要将PostreSQL服务器配置为在所有网络上侦听,则需要配置其主配置文件:
$ sudo nano /etc/postgresql/10/main/postgresql.conf
添加或者修改下面行:
listen_addresses = '*'
完成配置后,重新启动POSTRESQL 服务器:
$ sudo service postgresql restart
现在PostreSQL将在0.0.0.0:5432
上监听,使用ss确认:
$ ss -nlt State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 5 127.0.0.1:631 0.0.0.0:* LISTEN 0 128 0.0.0.0:5432 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 5 [::1]:631 [::]:*
要接受从远程PostreSQL客户端到所有数据库和所有用户的连接,请在“/etc/postgresql/10/main/pg_hba.conf”中添加以下行`
host all all 0.0.0.0/0 trust
否则可能会导致以下错误消息:
psql: FATAL: no pg_hba.conf entry for host "postresql-client", user "postgres", database "postgres", SSL on FATAL: no pg_hba.conf entry for host "postresql-client", user "postgres", database "postgres", SSL off
如果启用了UFW防火墙,则可以通过执行以下命令,将PostreSQL的端口“5432”打开到任何TCP传入流量:
$ sudo ufw allow from any to any port 5432 proto tcp Rule added Rule added (v6)
日期:2020-07-07 20:55:46 来源:oir作者:oir