Question

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?

Était-ce utile?

La solution

You need to create the database beforehand, create_all just creates the tables. To create database: sudo -u postgres createdb testData.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top