ubuntu 18 install postgresql-client 如何开启pg的远程访问,以及如何测试是否开通了?


因为ubuntu 18不再被官方支持,所以apt的安装源也该了。
原来的地址是是 http://apt.postgresql.org....
现在二级域名 apt 变成了  apt-archive. 归档了,不再更新了。

hw@t4:~$ sudo sh -c 'echo "deb https://apt-archive.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

hw@t4:~$ sudo apt install postgresql-client-14


[9679ae55-7257-436f-962d-0425d7f473ec] ActiveRecord::ConnectionNotEstablished (connection to server at "172.16.2.156", port 5432 failed: Connection refused
 Is the server running on that host and accepting TCP/IP connections?
):


如何开启pg的远程访问,以及如何测试是否开通了?

[de0ec5b1-e223-451c-a709-fbabf07bcdd3] ActiveRecord::NoDatabaseError (We could not find your database: tao_production. Which can be found in the database configuration file located at config/database.yml.

To resolve this issue:

- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.

To create your database, run:

        bin/rails db:create
):


sudo -u postgres pg_restore -Fc -d tao_production delitao.backup

psql -h 172.16.32.15 -U deploy -d tao_production


\l 
\c tao_production
\dt
\du

修改表的owner
delitao_production=# alter table  words  owner to deploy;


远程访问pg数据库的配置修改。
Step # 1: Login over ssh if server is outside your IDC
 
Step # 2: Enable client authentication

Once connected, you need edit the PostgreSQL configuration file, edit the PostgreSQL configuration file /var/lib/pgsql/data/pg_hba.conf (or /etc/postgresql/8.2/main/pg_hba.conf for latest 8.2 version) using a text editor such as vi.

Login as postgres user using su / sudo command, enter:
host all all 10.10.29.0/24 trust

Step # 3: Enable networking for PostgreSQL

You need to open PostgreSQL configuration file /var/lib/pgsql/data/postgresql.conf or /etc/postgresql/8.2/main/postgresql.conf.
listen_addresses='localhost'
Next set IP address(es) to listen on; you can use comma-separated list of addresses; defaults to ‘localhost’, and ‘*’ is all ip address:
listen_addresses='*'
Or just bind to 202.54.1.2 and 202.54.1.3 IP address
listen_addresses='202.54.1.2 202.54.1.3'

Step # 4: Restart PostgreSQL Server

Type the following command:
# /etc/init.d/postgresql restart
systemctl restart postgres


Step # 5: Iptables firewall rules

Make sure iptables is not blocking communication, open port 5432 (append rules to your iptables scripts or file /etc/sysconfig/iptables):

iptables -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d 10.10.29.50  --dport 5432 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -s 10.10.29.50 --sport 5432 -d 0/0 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT
Restart firewall:
# /etc/init.d/iptables restart


Step # 6: Test your setup

Use psql command from client system. Connect to remote server using IP address 10.10.29.50 and login using vivek username and sales database, enter:
$ psql -h 10.10.29.50 -U vivek -d sales


阅读量: 617
发布于:
修改于: