Postgresql

無法使用 URI 連接到 postgres,但可以使用 psql -U 連接

  • August 7, 2021

我正在嘗試在數字海洋上設置一個簡單的 Web 伺服器,但在使用 URI 使用 sqlalchemy 連接到數據庫時遇到問題。

跑步

root@maudlin:/server/http/maudlin# psql postgresql://maudlin:<password>@localhost/maudlin
psql: error: could not connect to server: Connection refused
       Is the server running on host "localhost" (127.0.0.1) and accepting
       TCP/IP connections on port 5432?

失敗但正在執行

root@maudlin:/server/http/maudlin# psql -U maudlin
Password for user maudlin: <password>
psql (12.7 (Ubuntu 12.7-0ubuntu0.20.04.1))
Type "help" for help.

maudlin=>

通過。

據我所知,我的 pg_hba.conf 文件允許本地 IP 連接:

# This file is read on server startup and when the server receives a
# SIGHUP signal.  If you edit the file on a running system, you have to
# SIGHUP the server for the changes to take effect, run "pg_ctl reload",
# or execute "SELECT pg_reload_conf()".
#
# Put your actual configuration here
# ----------------------------------
#
# If you want to allow non-local connections, you need to add more
# "host" records.  In that case you will also need to make PostgreSQL
# listen on a non-local interface via the listen_addresses
# configuration parameter, or via the -i or -h command line switches.
host    maudlin         maudlin         <personal ip 1>/32         md5
host    maudlin         maudlin         <personal ip 2>/32        md5


# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database superuser can access the database using some other method.
# Noninteractive access to all databases is required during automatic
# maintenance (custom daily cronjobs, replication, and similar tasks).
#
# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5

不是線

host    all             all             127.0.0.1/32            md5

意思是在本地主機上接受 ipv4 連接,密碼使用 md5?

我想我可以將機器的 ip 添加到外部 ip 的頂部列表中,並通過它路由我的連接,但接縫就像一個 Bad Idea™

有沒有人有任何調試提示或建議?

原來我已經修改了我listen_addressespostgresql.conf

改變

listen_addresses = '<machine ip>'         # what IP address(es) to listen on;

listen_addresses = '<machine ip>, localhost'         # what IP address(es) to listen on;

並重新啟動解決了問題。

引用自:https://unix.stackexchange.com/questions/663688