如何通过输入参数连接到 postgres 数据库?
How can I connect to postgres data base by input parameters?
我正在尝试使用 psycopg2 连接到 postgres 数据库。我通过输入询问用户和用户密码。但我认为,这种方式不让我连接到数据库。还有其他放置值的方法吗?
我尝试这样连接:
connection = psycopg2.connect("host=localhost dbname=dbname user=%s password=%s", (username, password))
这是我得到的错误:
connection = _connect(dsn, connection_factory=connection_factory, **kwasync)
TypeError: 'tuple' object is not callable
有几种方法可以从输入构建连接字符串,第一种是:
# From input
user_name = 'some_name'
pwd = 'some_pwd'
connection = psycopg2.connect(host=localhost, dbname=dbname, user=user_name, password=pwd)
第二个 Make dsn:
from psycopg2.extensions import make_dsn
dsn = make_dsn('host=localhost dbname=dbname', user=user_name, password=pwd)
connection = psycopg2.connect(dsn)
UPDATE,忘了最明显的方法:
dsn = 'host=localhost dbname=dbname user=' + user_name + ' password=' + pwd
connection = psycopg2.connect(dsn)
我正在尝试使用 psycopg2 连接到 postgres 数据库。我通过输入询问用户和用户密码。但我认为,这种方式不让我连接到数据库。还有其他放置值的方法吗?
我尝试这样连接:
connection = psycopg2.connect("host=localhost dbname=dbname user=%s password=%s", (username, password))
这是我得到的错误:
connection = _connect(dsn, connection_factory=connection_factory, **kwasync)
TypeError: 'tuple' object is not callable
有几种方法可以从输入构建连接字符串,第一种是:
# From input
user_name = 'some_name'
pwd = 'some_pwd'
connection = psycopg2.connect(host=localhost, dbname=dbname, user=user_name, password=pwd)
第二个 Make dsn:
from psycopg2.extensions import make_dsn
dsn = make_dsn('host=localhost dbname=dbname', user=user_name, password=pwd)
connection = psycopg2.connect(dsn)
UPDATE,忘了最明显的方法:
dsn = 'host=localhost dbname=dbname user=' + user_name + ' password=' + pwd
connection = psycopg2.connect(dsn)