Python 如何在psycopg2连接方法中指定架构?

Python 如何在psycopg2连接方法中指定架构?,python,postgresql,psycopg2,Python,Postgresql,Psycopg2,使用psycopg2模块使用python连接到PostgreSQL数据库。我能够使用下面的连接方法执行所有查询。现在,我想指定一个不同于public的模式来执行SQL语句。有没有办法在连接方法中指定架构名称 conn = psycopg2.connect(host="localhost", port="5432", user="postgres",

使用psycopg2模块使用python连接到PostgreSQL数据库。我能够使用下面的连接方法执行所有查询。现在,我想指定一个不同于public的模式来执行SQL语句。有没有办法在连接方法中指定架构名称

conn = psycopg2.connect(host="localhost",
                            port="5432",
                            user="postgres",
                            password="password",
                            database="database",
                            )
我试图直接在方法内部指定模式。
schema=“schema2”
但我得到以下编程错误

ProgrammingError: invalid dsn: invalid connection option "schema"

当我们在
psycopg2
中处理
ThreadConnectionPool
并创建连接池时,我们就是这样做的

from psycopg2.pool import ThreadedConnectionPool

db_conn = ThreadedConnectionPool(
    minconn=1, maxconn=5,
    user="postgres", password="password", database="dbname", host="localhost", port=5432,
    options="-c search_path=dbo,public"
)
您可以在params中看到
options
键。我们就是这样做的

from psycopg2.pool import ThreadedConnectionPool

db_conn = ThreadedConnectionPool(
    minconn=1, maxconn=5,
    user="postgres", password="password", database="dbname", host="localhost", port=5432,
    options="-c search_path=dbo,public"
)
当您使用光标从该连接执行查询时,它将按从左到右的顺序在
选项
中提到的模式中搜索,即
dbo,public

您可以尝试以下方法:

psycopg2.connect(host="localhost", 
                 port="5432", 
                 user="postgres", 
                 password="password", 
                 database="database", 
                 options="-c search_path=dbo,public")

希望这对您有所帮助。

如果您使用的是字符串表单,则需要URL转义
选项
参数:

postgresql://localhost/airflow?options=-csearch_path%3Ddbo,public
%3D
=的URL编码
=

例如,如果您正在使用SQLAlchemy,这会有所帮助