繁体   English   中英

Postgresql服务器远程连接

[英]Postgresql server remote connection

我正在尝试在ubuntu上设置一个postgresql 9.1服务器,用于远程访问数据。 我已经正确安装了postgres,服务器进程正在运行,我正在尝试对其进行配置,以便我可以通过Internet从局域网外的其他几台计算机远程访问服务器。

我已经修改了我的pg_hba.conf:

host    all     all     0.0.0.0     trust

和postgresql.conf:

listen_addresses = '*'
port = 5432

我还修改了我的iptables以接受端口5432上的连接。

当我尝试在python上使用psycopg2连接时:

conn=psycopg2.connect('host=XX.XX.XX.XX port=5432 dbname=postgres user=myUser password=mypassword')

我收到以下错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
connection_factory=connection_factory, async=async) 
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "XX.XX.XX.XX" and accepting
TCP/IP connections on port 5432?

我不确定我是否插入了正确的IP地址,我很好奇我将如何确切地知道在这里使用哪个IP地址连接到我的服务器。 我使用运行postgres服务器的计算机的公共IP,但我不确定是否正确。 或者还有其他一步我仍然缺席? 我知道这是不好的安全风格,但目前我想建立一个连接。 我的电脑也在路由器后面,所以我如何专门访问我的服务器?

任何帮助非常感谢。

你的pg_hba.conf不应该使用trust trust意味着不需要密码,我认为这不是你想要的。

这是正确的配置

host    all             all             0.0.0.0/0               md5

注意/0落后于0.0.0.0

完整的pg_hba.conf应该是这样的: -

local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
host    all             all             0.0.0.0/0               md5

请注意, trust仅适用于local连接。 即对于运行postgresql服务器的机器上运行在localhost IP 127.0.0.1上的应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM