After installing PostgreSQL database server, remote access mode is disabled by default for security reasons. However, sometimes you may want to allow remote connections to PostgreSQL database server from other locations, your home or office for example. In the next few lines I’ll guide you to do just that.
Connect to the remote server
First things first, you need to login to the remote server:
1 |
mirzap@bosnadev:~$ ssh root@remote-host |
Change The Listen Address
By default, PostgreSQL DB server listen address is set to the 'localhost' , and we need to change it so it accepts connection from any IP address; or you can use comma separated list of addresses. Here is how it looks by default:
1 2 |
root@fe35577e9f8b:/# grep listen /etc/postgresql/9.4/main/postgresql.conf listen_addresses = 'localhost' # what IP address(es) to listen on; |
Open your postgresql.conf file in your editor:
1 |
root@fe35577e9f8b:/# vim /etc/postgresql/9.4/main/postgresql.conf |
search for listen_addresses , and set it to '*' :
1 2 3 4 5 6 7 |
#------------------------------------------------------------------------------ # CONNECTIONS AND AUTHENTICATION #------------------------------------------------------------------------------ # - Connection Settings - listen_addresses = '*' # what IP address(es) to listen on; |
or if you want to set connection restrictions to a few IP’s, then you should set listen_addresses to something like this:
1 2 3 4 5 6 7 |
#------------------------------------------------------------------------------ # CONNECTIONS AND AUTHENTICATION #------------------------------------------------------------------------------ # - Connection Settings - listen_addresses = '192.168.1.100,192.168.1.101,192.168.1.110' # what IP address(es) to listen on; |
To find out more about connections and authentication and available parameters, check the official documentation page.
Open PostgreSQL to the world
In this step, you need to allow remote connections to actually reach your PostgreSQL server. Open pg_hba.conf :
1 |
root@fe35577e9f8b:/# vim /etc/postgresql/9.4/main/pg_hba.conf |
To allow connections from absolutely any address with password authentication add this line at the end of pg_hba.conf file:
1 |
host all all 0.0.0.0/0 md5 |
You can also use your network/mask instead just 0.0.0.0/0 .
Restart
You have made it! Just make sure to restart your PostgreSQL instance before leaving remote SSH session:
1 |
root@fe35577e9f8b:/# /etc/init.d/postgresql restart |
Now you should be able to connect to the PostgreSQL instance with any of DB tools.
Latest posts by Mirza Pasic (see all)
- Quick tip: How to delete a tag from a Git repository? - August 20, 2016
- Laravel Accessors and Mutators - December 17, 2015
- How to allow remote connections to PostgreSQL database server - December 15, 2015
Pingback: Cannot re-execute code until manually shutdown local PostgreSQL server – FeuTex – #ForAuthors()