Creating a postgresql DB using psycopg2

前端 未结 1 1410
太阳男子
太阳男子 2020-12-14 05:47

I\'m trying to create a postgres DB using a python script. Some research showed that using the psycopg2 module might be a way to do it. I installed it and made the required

相关标签:
1条回答
  • 2020-12-14 06:14

    PostgreSQL's client connects to a database named after the user by default. This is why you get the error FATAL: database "nishant" does not exist.

    You can connect to the default system database postgres and then issue your query to create the new database.

    con = connect(dbname='postgres', user='nishant', host='localhost', password='everything')
    

    Make sure your nishant user has permission to create databases.

    Edit: By the way, check out the ~/.pgpass file to store password securely and not in the source code (http://www.postgresql.org/docs/9.2/static/libpq-pgpass.html). libpq, the postgresql client librairy, check for this file to get proper login information. It's very very handy.

    0 讨论(0)
提交回复
热议问题