尝试通过ssh连接PostgreSQL,但出了点问题。有人可以帮忙吗?
from sshtunnel import SSHTunnelForwarder
import psycopg2
with SSHTunnelForwarder(
"hostname",
ssh_username="username",
ssh_password="password",
remote_bind_address=("127.0.0.1", 5432)) as server:
try:
conn = psycopg2.connect(host = "127.0.0.1", dbname="db_name", user="db_user", password="db_password")
cursor = conn.cursor()
cursor.execute("""SELECT * from table""")
rows = cursor.fetchall()
print(rows)
except Exception as e:
print("Uh oh, can't connect. Invalid dbname, user or password?")
print(e)
运行代码时 - 出错:
/usr/local/lib/python3.6/site-packages/psycopg2/ init .py:144:UserWarning:psycopg2 wheel包将从2.8版重命名;为了继续从二进制安装,请使用“pip install psycopg2-binary”代替。有关详细信息,请参阅:http://initd.org/psycopg/docs/install.html#binary-install-from-pypi。
""") Uh oh, can't connect. Invalid dbname, user or password? could not connect to server: Connection refused Is the server running on host
127.0.0.1and accepting **TCP/IP** connections on port
5432`?
多次检查数据库名称,用户和密码,但它现在正在运行。 通过ssh手动连接到db工作正常,但我需要在python中对分析师数据进行操作。