2014-12-02 164 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