python - PeeWee PostgreSQL 数据库创建 : does not exist

标签 python postgresql peewee

我正在尝试使用 PeeWee 创建 PostgreSQL 数据库。连接后出现以下错误:

File "peewee_test.py", line 44, in
psql_db.connect()
File "c:\python27\lib\site-packages\peewee.py", line 3602, in connect
self.initialize_connection(self._local.conn)
File "c:\python27\lib\site-packages\peewee.py", line 3514, in exit
reraise(new_type, new_type(*exc_args), traceback)
File "c:\python27\lib\site-packages\peewee.py", line 3600, in connect
**self.connect_kwargs)
File "c:\python27\lib\site-packages\playhouse\postgres_ext.py", line 385, in _connect
conn = super(PostgresqlExtDatabase, self)._connect(database, **kwargs)
File "c:\python27\lib\site-packages\peewee.py", line 3990, in _connect
conn = psycopg2.connect(database=database, **kwargs)
File "c:\python27\lib\site-packages\psycopg2__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
peewee.OperationalError: FATAL: database "test" does not exist

知道我做错了什么吗?

from peewee import *
from playhouse.postgres_ext import PostgresqlExtDatabase

psql_db = PostgresqlExtDatabase(
    'test',  # Required by Peewee.
    user='xxxxx',  # Will be passed directly to psycopg2.
    password='xxxxx',  # Ditto.
    host='',  # Ditto.
    port='5432'
)

psql_db.connect() # error occurs here

psql_db.create_tables([Person, Pet])

最佳答案

Peewee 不会为您创建数据库,您需要首先使用 psql shell 连接到数据库,例如管理员用户访问权限 psql --host HOST --port 5432 --username YOUR_USER -W -d postgres 并运行 CREATE DATABASE test;

Postgres 还有一个辅助 createdb 可执行文件:

createdb my_db

关于python - PeeWee PostgreSQL 数据库创建 : does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40445616/

相关文章:

Python lxml 屏幕抓取?

python - 如何在 Ubuntu 上将 mod_wsgi 安装到 xampp 服务器?遇到 libtool 错误

python - 重新启动 Bottle 应用程序(以编程方式)

python - 如何在 peewee 中的计算字段上创建索引

Python Peewee MySQL 批量更新

python - GET 表单方法上的 CSRF 验证错误

python - 如何使内置容器(集合、字典、列表)线程安全?

java - DSL 选择中的 JOOQ 和 PostgreSQL 类型

ruby-on-rails - Rails - 当时间差超过 18 小时时,如何在 Rails 中存储时间

node.js - Mac 上的 Node/Deno Postgres 客户端速度缓慢