我创建了一个 python 文件 app.py 并包含了连接到我在 postgresql 中创建的数据库的代码,如下所示:-
import psycopg2
conn = psycopg2.connect(
user='postgres',
password='1234',
host='localhost',
port='5432',
database='bubbleformation'
)
cursor = conn.sursor()
cursor.execute('SELECT * FROM bubbleformation')
for row in cursor: print(row)
conn.close()
这是按照 this medium article 中的说明进行的
但是,当我尝试在终端中执行此 python 文件时,出现以下错误:-
Traceback (most recent call last): File "app.py", line 8, in port='5432' File "/usr/lib/python2.7/dist-packages/psycopg2/init.py", line 130, in connect conn = _connect(dsn, connection_factory=connection_factory, **kwasync) psycopg2.OperationalError: FATAL: database "bubbleformation" does not exist
我已经创建了一个名为“bubbleformation”的表,可以通过终端在 psql 模式下查看它。
谁能帮我理解应该做什么?我尝试更改密码和用户权限,但都没有解决我的错误。
最佳答案
您应该创建同名“bubbleformation”的数据库和表。您可能已经在 postgres 数据库中创建了该表。
以 postgres 用户身份输入 psql 并调用 CREATE DATABASE bubbleformation;
,然后使用 \connect bubbleformation
连接到它,然后创建你的表(类似于 CREATE TABLE bubbleformation (id int, name text);
).
关于python - 数据库连接错误 : Database "dbname" does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58355369/