Home  >  Q&A  >  body text

python - psycopg2连接远程数据库被拒绝

psycopg2.OperationalError: could not connect to server: Connection refused
    Is the server running on host "45.32.1XX.2XX" and accepting
    TCP/IP connections on port 5432?

远程数据库端口已经打开了:

tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      11516/postgres                
tcp6       0      0 ::1:5432                :::*                    LISTEN      11516/postgres

网上查说要修改pg_hba.conf,但是我在postgresql根目录找不到这个文件,而且在我另一台我可以连接成功服务器上,也不存在这个文件。

求大神解答!感谢~

大家讲道理大家讲道理2716 days ago513

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 09:45:52

    It is recommended to read the official documentation:

    https://www.postgresql.org/do...

    Generally, the default rules of postgresql are very abnormal and only allow localhost connections. In your case, you really need to modify pg_hba.conf. As for the path of this file, it depends on different Linux distributions and the installation personnel's preferences, but it is certain It must exist, you can simply search for its path

    locate pg_hba.conf
    或者
    find / -type f -iname "pg_hba.conf" 2>/dev/null

    After finding it, modify the security policy. Basically, it has simple comments by default, which is easy to understand (release the local IP address)

    After modification, restart the postgresql service and it will be OK

    reply
    0
  • PHPz

    PHPz2017-04-18 09:45:52

    Only modify pg_hba.conf 文件是不够的,还要修改 postgresql.conflisten_addresses configuration items

    You can find the PGDATA of postgresql through show data_directory;

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:45:52

    Try to see if you can remotely log in to mysqld using mysql -uroot -p - P -hReference question: The linux terminal can connect to the mysql database, but it cannot connect to mysql through the php program

    reply
    0
  • Cancelreply