我尝试使用 postgres RDS 作为端点读取或写入 AWS RDS 代理。该操作适用于 psql,但在使用 pg8000 或 psycopg2 作为 Python 中的客户端库的同一客户端上失败。
如果我将 RDS 直接用作端点(没有 RDS 代理),则该操作适用于 pg8000 和 psycopg2。
sqlaclchemy/psycopg2错误消息:
Feature not supported: RDS Proxy currently doesn’t support command-line options.
我使用的代码的最小版本:
from sqlalchemy import create_engine
import os
from dotenv import load_dotenv
load_dotenv()
login_string = os.environ['login_string_proxy']
engine = create_engine(login_string, client_encoding="utf8", echo=True, connect_args={'options': '-csearch_path={}'.format("testing")})
engine.execute(f"INSERT INTO testing.mytable (product) VALUES ('123')")
pg8000:它停止/等待某事的地方在 core.py 中:
def sock_read(b):
try:
return self._sock.read(b)
except OSError as e:
raise InterfaceError("network error on read") from e
我使用的代码的最小版本:
import pg8000
import os
from dotenv import load_dotenv
load_dotenv()
db_connection = pg8000.connect(database=os.environ['database'], host=os.environ['host'], port=os.environ['port'], user=os.environ['user'], password=os.environ['password'])
db_connection.run(f"INSERT INTO mytable (data) VALUES ('data')")
db_connection.commit()
db_connection.close()
对于我提到的所有示例,RDS 代理中的日志看起来总是正常的 - 例如:
A new client connected from ...:60614.
Received Startup Message: [username="", database="", protocolMajorVersion=3, protocolMinorVersion=0, sslEnabled=false]
Proxy authentication with PostgreSQL native password authentication succeeded for user "" with TLS off.
A TCP connection was established from the proxy at ...:42795 to the database at ...:5432.
The new database connection successfully authenticated with TLS off.
我通过 RDS 和 RDS 代理上的安全组打开了所有端口,并在 VPC 中使用了 EC2。
我尝试打开和关闭自动提交。