我正试图在ubuntu上设置一个postgresql9.1服务器,用于远程访问数据。我已经正确安装了postgres,服务器进程正在运行,我正在尝试配置它,以便我可以从局域网外的其他几台计算机通过互联网远程访问服务器。
我已经用修改了我的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 IP127.0.0.1
上运行的应用程序。