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

标签 java android eclipse postgresql localhost

我正在开发一个 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/

相关文章:

android - Firebase 序列化/反序列化的命名约定?

java - 如何将旧的 Java 版本与新版本一起安装以在 eclipse spring 中使用?

java - JAXB - 未编码的字段为空

android - Pro guard错误构建发布apk

java - ImageIcon 通过使用字符串名称

android - 添加/删除 RelativeLayout(动画)的 View 时出现 NullPointerException

java hotspot(tm) 64 位服务器 vm 警告忽略选项 permsize=256m 支持已在 8.0 中删除

java - 带有图像的奇怪 FileNotFoundException

java - 将scala添加到intellij中现有的java模块中

java - 在内部从 Java 进行多个 REST 调用