gpt4 book ai didi

java - 无法连接到 PostgreSQL 数据库,错误代码 'localhost'

转载 作者:行者123 更新时间:2023-12-01 12:11:27 24 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,并且正在使用与 PostGIS bundle 在一起的 PostgreSQL。我想使用本地数据库,但通过“localhost”连接不起作用,我尝试了使用“127.0.0.1”的替代方案,但它不起作用,我收到以下错误:

org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

使用我的网络的 ipv4 地址可以正常工作,但我无法通过本地主机进行连接。我该如何解决这个问题?我的 pg_hba.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.



# TYPE DATABASE USER ADDRESS METHOD

# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 192.168.1.1/24 trust
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::1/128 trust

# Allow replication connections from localhost, by a user with the
# replication privilege.
#host replication postgres 127.0.0.1/32 trust
#host replication postgres ::1/128 trust

我的 postgresql.conf 看起来像:

# - Connection Settings -

listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5123 # (change requires restart)
max_connections = 100 # (change requires restart)

我在 Eclipse 中的代码看起来像

private String DB_URL = "jdbc:postgresql://" + Server.getDbserver() + ":" + Server.getDbport() + "/postgis";

我的服务器类中的变量看起来像。

private static String dbserver = "localhost";
private static int dbport = 5123;

总而言之,通过我的网络 ipv4(例如 192.168.1.199)连接可以工作,但通过“localhost”或“127.0.0.1”连接会生成上面发布的错误。

最佳答案

您为何使用 192.168.1.1/24 并信任?尝试一下也许是这样的:

host all all 192.168.1.1/32 md5

然后通过用户名/密码实现连接。

关于java - 无法连接到 PostgreSQL 数据库,错误代码 'localhost',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27246063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com