我正在尝试使用Python脚本创建一个Postgres数据库。一些研究表明,使用psycopg2模块可能是一种方法。我安装了它,并在pg_hba.conf文件中进行了必要的更改。我使用以下代码来创建数据库:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from psycopg2 import connect
import sys
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
con = None
con = connect(user='****', host = 'localhost', password='****')
dbname = "voylla_production1710"
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cur = con.cursor()
cur.execute('CREATE DATABASE ' + dbname)
cur.close()
con.close()
我尝试将con = connect(user='nishant', host = 'localhost', password='everything')
替换为con = connect(user='nishant', password='everything')
但是出现了以下错误:
con = connect(user='nishant', host = 'localhost', password='everything')
File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
connection_factory=connection_factory, async=async)
psycopg2.OperationalError: FATAL: database "nishant" does not exist
有人能告诉我正确的做法吗。 谢谢
set_isolation_level
对我来说是必要的一步,让它正常工作。 - wordsforthewise