[英]Cannot connect to PostgreSQL database as 'localhost'
我正在开发一个Android应用程序,并且正在使用与PostGIS捆绑在一起的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";
我的Server类中的变量看起来像。
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
然后用用户名/密码实现连接。
好了,您需要在Android设备和计算机之间建立连接,但是不幸的是,USB电缆无法实现这一点。 但是,如果您确实想使应用程序脱机,则必须使PC或Android设备成为热点。 我不建议您将平板电脑设置为热点,但将计算机设置为热点非常简单,您可以使用Connectify和voila,您的笔记本电脑与计算机连接到同一LAN,并且您将获得计算机ipv4通过Connectify本身。 玩得开心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.