问题
I am using the following code to create postgresql database using sqlalchemy:
engine=create_engine('postgresql+psycopg2://postgres@localhost/testData')
Base.metadata.create_all(engine)
But it gives me the following error even though I manually created the database in psql:
File "/home/ubuntu/venve/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 376, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/home/ubuntu/venve/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
sqlalchemy.exc.OperationalError: (OperationalError) FATAL: database "testData" does not exist
Why is this happening?
回答1:
You need to create the database beforehand, create_all
just creates the tables. To create database: sudo -u postgres createdb testData
.
来源:https://stackoverflow.com/questions/61121082/db-create-all-doesnt-create-a-database