简体   繁体   English

以postgres用户身份连接到PostgreSQL时出现连接错误?

[英]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.

相关问题 Docker- django 在连接到 postgres 时抛出错误:psycopg2.OperationalError:无法连接到服务器:连接被拒绝 - Docker- django throws error while connecting to postgres: psycopg2.OperationalError: could not connect to server: Connection refused 使用 sqlalchemy 连接到 postgresql 时出错 - Error while connecting to postgresql using sqlalchemy 连接到数据库/postgresql/python 时出错 - Error while connecting to Database/postgresql/python 将python连接到elasticsearch时出现连接错误 - connection error while connecting python to elasticsearch 通过python连接mysql数据库时出现连接错误 - Connection Error while connecting the mysql database through python Spark JDBC 连接到 PostgreSQL 时出错 - Spark JDBC error connecting to PostgreSQL SQL 字符串错误:“连接到 PostgreSQL 运算符时出错不存在:日期 = 整数” - Error with SQL string: "Error while connecting to PostgreSQL operator does not exist: date = integer" python 在连接到 PostgreSQL 时不能使用 .pgpass - python can not use .pgpass while connecting to PostgreSQL PostgreSQL - 用户“postgres”的对等身份验证失败 - PostgreSQL - Peer authentication failed for user “postgres” Postgres用户错误-依赖关系 - Postgres User Error - Dependencies
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM