bosnadev

№ 19 · 2015-12-15 · 2 min · databases

How to allow remote connections to PostgreSQL database server

Allowing remote connections to PostgreSQL 9.4: set listen_addresses in postgresql.conf, add a host line to pg_hba.conf and restart the server.

written before the past year · entry 19 of 31 · 10 in the past year

This article is out of date
Written for PostgreSQL 9.4 in 2015.

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:

terminal1 line
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:

terminal2 lines
root@fe35577e9f8b:/# grep listen /etc/postgresql/9.4/main/postgresql.conflisten_addresses = 'localhost'		# what IP address(es) to listen on;

Open your postgresql.conf file in your editor:

terminal1 line
root@fe35577e9f8b:/# vim /etc/postgresql/9.4/main/postgresql.conf

search for listen_addresses , and set it to '*' :

/etc/postgresql/9.4/main/postgresql.conf7 lines
#------------------------------------------------------------------------------# 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:

/etc/postgresql/9.4/main/postgresql.conf7 lines
#------------------------------------------------------------------------------# 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 :

terminal1 line
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:

/etc/postgresql/9.4/main/pg_hba.conf1 line
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:

terminal1 line
root@fe35577e9f8b:/# /etc/init.d/postgresql restart

Now you should be able to connect to the PostgreSQL instance with any of DB tools.

Related

How To Redirect www To non-www And Vice Versa with NginxserverSSH Authentication With Keys Instead PasswordsserverSetting Up Laravel Environmentslaravel