简体   繁体   English

无法以“ localhost”连接到PostgreSQL数据库

[英]Cannot connect to PostgreSQL database as 'localhost'

I am developing an Android application and I'm using PostgreSQL bundled with PostGIS. 我正在开发一个Android应用程序,并且正在使用与PostGIS捆绑在一起的PostgreSQL。 I want to use a local database for that but connecting via 'localhost' doesn't work, I have tried the alternative to use '127.0.0.1' instead but it is not working, I am getting the following error: 我想为此使用本地数据库,但无法通过“ 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.

Using my network's ipv4 adress works, but I cannot connect via localhost. 使用我的网络的ipv4地址可以使用,但是我无法通过本地主机连接。 How can I fix that? 我该如何解决? My pg_hba.conf looks like: 我的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

and my postgresql.conf looks like: 和我的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)

My code in Eclipse looks like 我在Eclipse中的代码看起来像

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

And my variables inside my Server class look like. 我的Server类中的变量看起来像。

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

As a summary, connecting via my network ipv4 (for example 192.168.1.199) works, but connecting via 'localhost' or '127.0.0.1' generates me the error I posted above. 综上所述,通过我的网络ipv4(例如192.168.1.199)进行连接是可行的,但是通过“ localhost”或“ 127.0.0.1”进行连接会产生我上面发布的错误。

Why are you using 192.168.1.1/ 24 and trust? 为什么使用192.168.1.1/ 24和信任? Try it maybe like this: 尝试像这样:

host all all 192.168.1.1/32 md5

And then implement the connection with username / password. 然后用用户名/密码实现连接。

Well, you need a connection between your Android device and your computer but unfortunately, the USB cable does not do that. 好了,您需要在Android设备和计算机之间建立连接,但是不幸的是,USB电缆无法实现这一点。 However, if you do want to rule your application offline, you will have to make your PC or your Android device a hotspot. 但是,如果您确实想使应用程序脱机,则必须使PC或Android设备成为热点。 I would not reccomend you to make your tablet a hotspot, but making a computer a hotspot is pretty easy, you can use Connectify and voila, your laptop is connected to the same LAN as your computer and you will get the computer ipv4 provided to you by Connectify itself. 我不建议您将平板电脑设置为热点,但将计算机设置为热点非常简单,您可以使用Connectify和voila,您的笔记本电脑与计算机连接到同一LAN,并且您将获得计算机ipv4通过Connectify本身。 Have fun. 玩得开心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM