Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法连接到psycopg2,但可以通过命令行进行连接_Python_Postgresql_Heroku_Psycopg2 - Fatal编程技术网

Python 无法连接到psycopg2,但可以通过命令行进行连接

Python 无法连接到psycopg2,但可以通过命令行进行连接,python,postgresql,heroku,psycopg2,Python,Postgresql,Heroku,Psycopg2,我正在尝试连接到远程Postgres数据库(在本例中是Heroku Postgres实例)。我有一个Fabric命令,它使用psycopg2对数据库执行一些工作 我的Postgres连接是这样发生的: # conf is a hash derived from the heroku config DATABASE_URL self.conn = psycopg2.connect( database=conf.get('database'), # supplied by heroku

我正在尝试连接到远程Postgres数据库(在本例中是Heroku Postgres实例)。我有一个Fabric命令,它使用
psycopg2
对数据库执行一些工作

我的Postgres连接是这样发生的:

# conf is a hash derived from the heroku config DATABASE_URL
self.conn = psycopg2.connect(
    database=conf.get('database'), # supplied by heroku
    user=conf.get('username'), # is the database username supplied by heroku
    password=conf.get('password'), # supplied by heroku
    host=conf.get('host'), # e.g ec2-00-000-00-00.compute-1.amazonaws.com
    port=conf.get('port') # 5492
)
运行脚本时出错:

psycopg2.OperationalError: FATAL:  password authentication failed for user "my-server-user"
FATAL:  no pg_hba.conf entry for host "my-ip-address-here", user "my-server-user", database "database-name-here", SSL off
在调查pg_hba.conf时,我临时引入了以下行:

host    all             all             trust
重新开始深造

sudo /etc/init.d/postgresql-9.3 restart
但我仍然面临这个问题。但是,我可以使用命令行客户端进行简单连接(即使不更改pg_hba配置):

手动提供密码

代码在我的Mac上本地运行,因此存在配置错误或被阻止的情况。我就是想不出可能是什么。欢迎任何建议


(显然,在上面的示例中,实际值已替换为占位符)

Heroku要求您使用SSL

如果您正在连接Heroku,则:

  • 在本地主机上修改PostgreSQL server的
    pg_hba.conf
    将完全没有效果,因为您连接的不是该服务器

  • 您必须使用SSL。将
    sslmode='require'
    选项传递给
    psycopg2


这毫无意义。您正在使用Heroku,那么如何修改
pg_hba.conf
?您是否在本地计算机上修改了不相关的PostgreSQL server的
pg_hba.conf
?或者你不是真的想用希罗库吗!我知道这一定是件傻事。我刚刚得到了一大堆关于我收到的错误的答案,没有点击pg_hba.conf显然不会影响我的连接。今天早上我会测试一下,然后接受你的答案。这是有效的,我发现我提供的用户名也有问题。把这归咎于盯着这个问题太久了。谢谢,标记为已接受。@JeffAndersen如果未指定用户名,通常会发生这种情况,因此会选择默认值(unix用户psycopg2当前作为运行)。也许您在分配
user=
时犯了一个错误?(显然你是在我写这篇文章并编辑评论时发现的,耸耸肩)
psql -h ec2-00-000-00-00.compute-1.amazonaws.com -p 5492 database-name -W