Python3:使用 SSL 连接到远程 Postgres 数据库
Python3: Connect to Remote Postgres Database with SSL
我正在设置远程 PostgreSQL 数据库。服务器是 运行 CentOS 7 和 PostgreSQL-9.5。目前,我正在测试用户是否可以查询数据库。为此,我有以下几点:
import psycopg2
host = 'server1'
dbname = 'test_db'
user = 'test-user'
sslcert = 'test-db.crt'
sslmode = 'verify-full'
sslkey = 'test-db.key'
dsn = 'host={0} dbname={1} user={2} sslcert={3} sslmode={4} sslkey={5}'.format(host, dbname, user, sslcert, sslmode, sslkey)
conn = psycopg2.connect(dsn)
连接超时,出现以下错误:
psycopg2.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "server1" (xx.xx.xx.xx) and accepting
TCP/IP connections on port 5432?
我已经尝试了几种方法(如下所示)。我试图确定问题存在于哪一侧:Python 端或数据库配置:
- Python 语法正确吗?
- 在哪里可以找到有关 DSN 参数的文档,例如
sslmode
、sslcert
和 sslkey
?
- 是否有更适合这种连接的其他软件包?
- 我还应该问什么其他问题?
我检查了以下内容:
- 'server1'输入正确,Python返回的IP地址对应
- 所有其他参数拼写正确并引用正确的对象
- Postgres 目前 运行(
service postgres-9.5 status
显示 "active")
- Postgres 正在侦听端口 5432(
netstat -na | grep tcp
在端口 5432 上显示 "LISTEN")
- SSL 是 运行 我的 table (
psql -U username -W -d test-db -h host
returns SSL connection (protocol: TLSAv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
user=test-user
已作为超级用户添加到 postgres
我的理解是 psycopg2
是现在使用的合适的包。我搜索了 documentation and don't find much information regarding SSL connections. I found this SO post,其中讨论了使用 psycog2
的 SSL 连接,但我无法将某些语法与文档相匹配。
在 Python 脚本中,我尝试了以下所有 4 种组合:
- 使用
sslmode='require'
- 使用
test-db.crt
和 test-db.key
的绝对路径
看来你给自己介绍了一个False Dilemma。问题不仅仅存在于 Python 和数据库配置之间。之间存在其他实体可能会导致断开连接。
- Is the Python syntax correct?
是的。 psycopg2.connect()
文档中描述了语法。它具有以下形式:
psycopg2.connect(dsn=None, connection_factory=None, cursor_factory=None, async=False, **kwargs)
其中 DSN (Data Source Name) 可以作为单个字符串或单独的参数给出:
conn = psycopg2.connect(dsn="dbname=test user=postgres password=secret")
conn = psycopg2.connect(dbname="test", user="postgres", password="secret")
- Where can I find documentation concerning the DSN arguments, such as sslmode, sslcert, and sslkey?
请注意,作为 DSN 参数,它们不是 psycopg2
模块的一部分。它们由数据库定义,在本例中为 Postgres。它们可以在 Parameter Key Words 部分下的数据库连接控制函数一章中找到。
- What other questions should I be asking?
也许,
主机(PostgresSQL 服务器)和客户端(本地 Python 实例)之间是否存在任何可能阻止通信的东西?
一个答案是 "the firewall." 这就是问题所在。 Postgres 正在倾听,Python 正在伸出援手。但是门是关着的。
我正在设置远程 PostgreSQL 数据库。服务器是 运行 CentOS 7 和 PostgreSQL-9.5。目前,我正在测试用户是否可以查询数据库。为此,我有以下几点:
import psycopg2
host = 'server1'
dbname = 'test_db'
user = 'test-user'
sslcert = 'test-db.crt'
sslmode = 'verify-full'
sslkey = 'test-db.key'
dsn = 'host={0} dbname={1} user={2} sslcert={3} sslmode={4} sslkey={5}'.format(host, dbname, user, sslcert, sslmode, sslkey)
conn = psycopg2.connect(dsn)
连接超时,出现以下错误:
psycopg2.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "server1" (xx.xx.xx.xx) and accepting
TCP/IP connections on port 5432?
我已经尝试了几种方法(如下所示)。我试图确定问题存在于哪一侧:Python 端或数据库配置:
- Python 语法正确吗?
- 在哪里可以找到有关 DSN 参数的文档,例如
sslmode
、sslcert
和sslkey
? - 是否有更适合这种连接的其他软件包?
- 我还应该问什么其他问题?
我检查了以下内容:
- 'server1'输入正确,Python返回的IP地址对应
- 所有其他参数拼写正确并引用正确的对象
- Postgres 目前 运行(
service postgres-9.5 status
显示 "active") - Postgres 正在侦听端口 5432(
netstat -na | grep tcp
在端口 5432 上显示 "LISTEN") - SSL 是 运行 我的 table (
psql -U username -W -d test-db -h host
returnsSSL connection (protocol: TLSAv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
user=test-user
已作为超级用户添加到 postgres
我的理解是 psycopg2
是现在使用的合适的包。我搜索了 documentation and don't find much information regarding SSL connections. I found this SO post,其中讨论了使用 psycog2
的 SSL 连接,但我无法将某些语法与文档相匹配。
在 Python 脚本中,我尝试了以下所有 4 种组合:
- 使用
sslmode='require'
- 使用
test-db.crt
和test-db.key
的绝对路径
看来你给自己介绍了一个False Dilemma。问题不仅仅存在于 Python 和数据库配置之间。之间存在其他实体可能会导致断开连接。
- Is the Python syntax correct?
是的。 psycopg2.connect()
文档中描述了语法。它具有以下形式:
psycopg2.connect(dsn=None, connection_factory=None, cursor_factory=None, async=False, **kwargs)
其中 DSN (Data Source Name) 可以作为单个字符串或单独的参数给出:
conn = psycopg2.connect(dsn="dbname=test user=postgres password=secret")
conn = psycopg2.connect(dbname="test", user="postgres", password="secret")
- Where can I find documentation concerning the DSN arguments, such as sslmode, sslcert, and sslkey?
请注意,作为 DSN 参数,它们不是 psycopg2
模块的一部分。它们由数据库定义,在本例中为 Postgres。它们可以在 Parameter Key Words 部分下的数据库连接控制函数一章中找到。
- What other questions should I be asking?
也许,
主机(PostgresSQL 服务器)和客户端(本地 Python 实例)之间是否存在任何可能阻止通信的东西?
一个答案是 "the firewall." 这就是问题所在。 Postgres 正在倾听,Python 正在伸出援手。但是门是关着的。