试图连接到python shell中的postgresql但我遇到了一些问题。
我正在使用Windows子系统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数据库中创建表,但我似乎无法解决这个错误。
你试过在psycopg2.connect()中添加主机和密码和端口,如:conn = psycopg2.connect(database="postgres", user="postgres" , password="your_db_user_password" , host="127.0.0.1")
?