Psycopg2操作错误:无法连接到服务器:没有这样的文件或目录

时间:2019-04-14 23:08:00

标签: python postgresql shell psycopg2

试图在python shell中连接到postgresql,但是我遇到了一些问题。

我正在使用Windows Subsystem for Linux。我尝试将端口更改为5433而不是5432。并尝试设置host =“ / tmp /”。这些东西都不起作用。

我尝试运行它。

>>> import psycopg2
>>> conn = psycopg2.connect(dbname="postgres", user="postgres")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/anaconda3/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

数据库“ postgres”出现在数据库列表中:

                              List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges
-----------+----------+----------+---------+---------+-----------------------
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres

我希望能够在postgres数据库中创建表,但似乎无法解决此错误。

1 个答案:

答案 0 :(得分:0)

您是否尝试在psycopg2.connect()中添加主机,密码和端口,例如:conn = psycopg2.connect(database="postgres", user="postgres" , password="your_db_user_password" , host="127.0.0.1")