postgres 和 sqlite 的等效连接

loc*_*boy 0 python sqlite postgresql

是否有一种等效的方法来连接到 postgres 数据库,就像 sqlite 使用 python 连接到数据库一样?

例如,在 sqlite 中,连接将由 定义conn = sqlite3.connect(curruser.dbname)。postgres 的类似连接语法是什么?

alf*_*alf 6

然后您可以使用psycopg连接器,连接语法将类似,只是您需要在连接字符串中指定更多信息:

conn_string = "host='localhost' dbname='my_database' user='postgres' password='secret'"
conn = psycopg2.connect(conn_string)
Run Code Online (Sandbox Code Playgroud)

以下是一些示例:Using psycopg2 with PostgreSQL