2014-12-02 161 views
0

我正在开发一个Android应用程序,并且我正在使用与PostGIS捆绑在一起的PostgreSQL。我想使用本地数据库,但通过'本地主机'连接不起作用,我试过替代使用'127.0.0.1',而不是它的工作,我得到以下错误:无法连接到PostgreSQL数据库作为'localhost'

org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. 
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)的作品连接,而是通过“本地主机”或“127.0.0.1”生成我,我上面贴的错误连接。

+0

那么,PostgreSQL实际上在Android OS *内运行*? – 2014-12-02 09:57:38

+0

我不知道我明白你想说什么。 – MrSilent 2014-12-02 10:19:02

+0

这是http://dba.stackexchange.com/questions/83851的转贴。在Android上安装postgresql **服务器**是个不小的功夫,请参阅[将Postgresql服务器移植到Android](https://groups.google.com/forum/#!topic/pgandroid/STg8xObGFlo)并且它不能在不知道你在做什么的情况下完成。 – 2014-12-02 10:22:09

回答

0

那么,你需要一个连接你的Android设备和你的电脑,但不幸的是,USB电缆不这样做。 但是,如果您想要脱机管理应用程序,则必须将PC或Android设备设置为热点。我不建议你让平板电脑成为热点,但让电脑成为热点非常容易,你可以使用Connectify和瞧,你的笔记本电脑连接到与你的电脑相同的局域网,你会得到提供给你的计算机ipv4由Connectify自己。 玩得开心。

+0

谢谢你的回答,这是我一直在寻找的,我会尝试你所说的。 – MrSilent 2014-12-04 15:14:51

1

为什么使用192.168.1.1/ 并相信?尝试它可能是这样的:

host all all 192.168.1.1/32 md5 

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

+0

谢谢你的观察,罗宾。但这并不能帮助我的事业。 – MrSilent 2014-12-03 08:43:19