Cannot connect to postgres from remote host

匿名 (未验证) 提交于 2019-12-03 01:29:01

问题:

I have a database server (192.168.1.50) running postgres. I have created a database named "testdb" and a user "testuser" with password "testuserpw".
Locally, I can connect to the db using:

psql -d testdb -U testuser

When I issue the command from another host (192.168.1.60):

psql -h 192.168.1.50 -d testdb -U testuser

I have the error:

psql: could not connect to server: Connection refused Is the server running on host "192.168.1.50" and accepting TCP/IP connections on port 5432?

Any idea ?

回答1:

Check the setting of listen_addresses in your postgresql.conf file. Many distributions make it default to 127.0.0.1, i.e. listen only to connections coming in from localhost. It should be set to '*' to listen for connections on all interfaces.

If you are still having trouble, use lsof to see what network sockets the postgres process is listening on.



回答2:

On Ubuntu, I noticed that remote access at some point stopped working (currently using 9.1.9). The reason is, that postgres is no longer started with the -i switch [1] so no matter what you configure for listen_addresses, it will be ignored.

Fortunately, adding the following line to /etc/environment solves the problem after logging out and in again (or reboot):

PGOPTIONS="-i"

See [2] for more options. Note, that adding this to /etc/postgresql/9.1/main/environment did NOT work for me.

Now, when doing nmap ip-of-my-remote-server I finally get this again:

5432/tcp open  postgresql

Yay!

[1] http://www.postgresql.org/docs/9.1/static/runtime-config-short.html

[2] http://www.postgresql.org/docs/9.1/static/libpq-envars.html



回答3:

Is the firewall letting the connections through? Or, check if pg_hba.conf allows connecting from addresses other than localhost.



回答4:

The listen_address configvar in postgresql.conf is not the only way to get postgres to listen on the non-local IP-address (or addresses).

Use option "-o -h *" if you start postgres from pg_ctl, otherwise do add "-h" "*" to the postgres command line, like e.g.

/usr/local/pgsql/bin/postgres -D /pg/data "-h" "*"

Of course /pg/data must be changed to your current datapath.

This is especially useful when experimenting.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!