简体   繁体   中英

how to specify host using ip address instead of name to connect to postgres database using netbeans in a java project?

i want to add database to my netbeans java project using ip address.
my question is that when i specify hostname by name that is "localhost" followed my correct details in the new connection wizard in netbeans connection establishes succesfully. this is as shown below. 按名称主持

but when i specify host by specifying ip address of my own computer that is localhost connection doesn't get established. same is shown below IPCONFIG托管我的IP

well my motive is not to put ip of localhost but of another pc for remote database sharing over internet. but i think the problem lies in the method of specifying host using ip. if i get it right database sharing might work between different hosts as well.
also any other methods of sharing postgres database over the internet are also welcome.
i am a newbie in java as well as netbeans so any help is appreciated.

Your sense of thinking that localhost is your IP-Address is itself wrong!You need to have a loop-back address which will direct your operations to itself and not as the case of your IP-Address shown in ipconfig command.

You need to specify this 127.0.0.1 as the IP-Address. 127.0.0.1 is the most-commonly used IPv4 loopback address.

Try this :- jdbc:postgresql://127.0.0.1:5432/nic_project_db

localhost means this computer. It is a hostname that the computer's software and users may employ to access the computer's own network services via its loopback network interface. Using the loopback interface bypasses local network interface hardware. // Taken from Wikipedia.

For future reference to anyone that might have same question here is solution:

In the postgresql database folder open up the postgresql.conf file. Add the following line:

listen_addresses = '*'

Then open up the pg_hba.conf file and add the following line:

host    all             all             0.0.0.0/0               md5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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