Configure PostgreSQL to accept connections from network

wrote by Rafael Marangoni, from Consultoria Linux team.

By default, on some distros, PostgreSQL will only accept connections from localhost. When you have only access from localhost (from localhost Apache, by example) everything is ok, but when you need that postgresql accepts connections for other hosts, you need to make some configs.

First of all, edit the postgresql.conf file (on CentOS the default location is /var/lib/pgsql/data/postgresql.conf).

vi /var/lib/pgsql/data/postgresql.conf

Search the following line:

listen_addresses = 'localhost'

Change it to:

listen_addresses = '*'

Secondly, you need to change the permissions inside pg_hba.conf file (on CentOS, the default location is /var/lib/pgsql/data/pg_hba.conf)

vi /var/lib/pgsql/data/pg_hba.conf

Include the following line (at the end of the file):

host    username    all     md5

username: it's the name of the postgres user

all: the database name (here we enabled all of them) is the IP address/subnet to accept connections
md5: is the method of authentication (md5 requests password)

Leave a Comment