How to connect to a PostgreSql Database over the network

Author: Veeresh Rudrappa. Date: March 10, 2013

If your database is stored in a different machine than your development machine then you can connect to it from your application over the network. You might have just bought a new laptop and might not want to the take the strain of setting up the database on your new machine all over again. So Let us see how to connect to PostgreSql over the network in few easy steps.

Step 1: Navigate to your postgreSql installation folder. Locate pg_hba.conf and postgresql.conf, on your computer.
On My computer it is located at C:\Program Files (x86)\PostgreSQL\8.4\data. Copy those two files and back it up somewhere else so that you can always replace it back if you mess up something.

Step 2: Open your pgAdmin management tool. Select File and click on open postgresql.conf. It will ask for the location and you know where it is now. C:\Program Files (x86)\PostgreSQL\8.4\data.

postgresql.conf

Note: Change the Value of listen_address. Update it with your current IP Address. You can look it up in command prompt by typing ipconfig.

Add IP address

Step 3: Open pga_hba.conf. Here you need to update which IP address to allow access for. I have added 192.168.1.0/24. It would allow any connection from within my subnet which have starting IP Address as 192.168.1.XX .

pg_hba.conf

Add Subnet

Step 4: Next you would have to restart your postgresql windows service.
Go to Control Panel > Administrative tools > Services. Locate the postgresql service and restart it.

Step 5: In the web.config of your development machine, update connection string with the new server address.

                                    
                                    

Hope this helps. :)
comments powered by Disqus