python - 数据库连接错误 : Database "dbname" does not exist

标签 python postgresql psycopg2 dbconnection

我创建了一个 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/

相关文章:

python - Scrapy SgmlLinkExtractor如何使用正则表达式定义规则

Python:列表中 Nan 的相等性?

python - 如何使用 Python 的 OpenSSL 库从中创建 RSA 对和 CRS?

PHP password_hash() 与 Postgres crypt()

python - 为什么 PostgreSQL cursor.execute() 命令要加单引号?

python - 匹配字符串末尾的模式?

java - 尝试在 PostGres 中执行 array_append 函数时出现类型转换异常

sql - 使用 PGX 在 Golang 中进行交易

linux - 为 Postgres 中的 to_char 设置全局标准

python - Postgres SSL SYSCALL 错误 : EOF detected with python and psycopg