[英]Connection Error while connecting to PostgreSQL as postgres user?
I am not able connect to PostgreSQL remotely using python and psycopg2: 我无法使用python和psycopg2远程连接到PostgreSQL:
Here is my code. 这是我的代码。
>>> import psycopg2
>>> conn_string = "host='localhost' dbname='mydb' user='postgres'"
>>> print "Connecting to database\n ->%s" % (conn_string)
Connecting to database
->host='localhost' dbname='mydb' user='postgres'
>>> conn = psycopg2.connect(conn_string)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/tools/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? 无法连接到服务器:连接被拒绝服务器是否在主机“ localhost”(127.0.0.1)上运行并在端口5432上接受TCP / IP连接?
The password is not set for postgres user. 没有为postgres用户设置密码。
Generally, I can connect to database by running following method on host. 通常,我可以通过在主机上运行以下方法来连接数据库。
1. SSH to box
2. su - postgres
3. psql
4. \c mydb
The server runs PostgreSQL 9.1. 服务器运行PostgreSQL 9.1。
You're trying to connect to PostgreSQL on localhost
using a script running on your computer, but there's no PostgreSQL server running there. 您正在尝试使用计算机上运行的脚本连接到
localhost
上的PostgreSQL,但是那里没有运行PostgreSQL服务器。
For this to work, you'd have to ssh to the remote server, then run your Python script there, where the PostgreSQL server is "local" relative to the Python script. 为此,您必须SSH到远程服务器,然后在其中运行Python脚本,其中PostgreSQL服务器相对于Python脚本是“本地”的。
(That's why running psql
works - because you're running it on the remote server , where PostgreSQL is "local" relative to psql
). (这就是为什么运行
psql
的原因-因为您是在PostgreSQL相对于psql
是“本地” 的远程服务器上运行它)。
Alternately, you could: 或者,您可以:
Use an SSH tunnel to forward the PostgreSQL port from the local computer to the remote one; 使用SSH隧道将PostgreSQL端口从本地计算机转发到远程计算机; or
要么
Connect directly over TCP/IP to the remote PostgreSQL server using its host name or IP address, after enabling remote connections on the server. 在服务器上启用远程连接后,使用其主机名或IP地址直接通过TCP / IP连接到远程PostgreSQL服务器。
Note that just putting the server's IP address or host name into the connection string instead of localhost
will not work unless you also configure the server to accept remote connections . 请注意,除非将服务器配置为接受远程连接,否则仅将服务器的IP地址或主机名放入连接字符串而不是
localhost
不会起作用 。 You must set listen_addresses
to listen for non-local connections, add any required firewall rules, set pg_hba.conf
to permit connections from remote machines, and preferably set up SSL. 您必须设置
listen_addresses
来侦听非本地连接,添加任何必需的防火墙规则,设置pg_hba.conf
以允许来自远程计算机的连接,并且最好设置SSL。 All this is covered in the Client Authentication chapter of the PostgreSQL user manual. 所有这些都在PostgreSQL用户手册的“客户端身份验证”一章中介绍。
You'll probably find an SSH tunnel simpler and easier to understand. 您可能会发现更简单,更容易理解的SSH隧道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.