1
votes

I'm attempting to create a PostgreSQL database using PeeWee. Upon connecting I get the following error:

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

Any idea what I am doing wrong?

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])
1
Are you sure you have the database test configured and the current user/password/host you are using have privileges to access this database?Dekel
@Dekel I know my username and pass are correct. However the PostgreSQL db doesn't exist. I thought PeeWee would create it for me? I am running my script locally and expect/want my db to be local aswellsazr
Ok my silly mistake. Once I create the database everything is finesazr

1 Answers

1
votes

Peewee wont create the database for you, you need to first connect to the database with psql shell for example and admin user access psql --host HOST --port 5432 --username YOUR_USER -W -d postgres and run CREATE DATABASE test;

Postgres has also a helper createdb executable:

createdb my_db