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

rah*_*gal 3 python postgresql shell psycopg2

尝试在 python shell 中连接到 postgresql 但遇到了一些问题。

我正在使用适用于 Linux 的 Windows 子系统。我尝试将端口更改为 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"?
Run Code Online (Sandbox Code Playgroud)

数据库“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
Run Code Online (Sandbox Code Playgroud)

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

WaL*_*oui 6

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