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

3

我试图在Python shell中连接到PostgreSQL,但遇到了一些问题。

我正在使用Windows Subsystem for Linux。我尝试将端口更改为5433而不是5432,并尝试设置主机为“/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

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


你好,欢迎来到 Stack Overflow!您是否尝试使用指定的参数/用户/密码通过命令行连接? - Taryn East
1个回答

6

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接