PostgreSQL with SQLalchemy database is not found

梦想与她 提交于 2021-02-11 17:21:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!