Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a postgres database on one server and I need access it from another server.

I need to edit the pg_hba.conf file, but have now idea what are the steps to edit this file. Server Centos

I need to add the following line in the file

host    all         all         10.0.2.12         255.255.255.255   trust

I located it in var/lib/pgsql/data/

Now basically I'm not sure what are the correct steps to do this

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

First take a backup copy of the given pg_hba.conf

sudo tar cvf /var/tmp/mybackup_pg_hba_conf.tar /var/lib/pgsql/data/pg_hba.conf

That way you have the older version ready if you screw up. A nice feature of tar archive is that preserves permissions and directories. So when the pg_hba.conf is broken beyond recognition. You can:

cd /
sudo tar xvf /var/tmp/mybackup_pg_hba_conf.tar

This will restore the backup pg_hba.conf from the archive you created earlier.

As for the actual insertion of line:

sudo su -c 'echo "host    all         all         10.0.2.12         255.255.255.255   trust" >> /var/lib/pgsql/data/pg_hba.conf'

(NOTE: Command edited. Thanks bortzmeyer!)

Should do it. Note that this just appends the line into the pg_hba.conf. If you ever need to change it you have to use an actual editor. Like vi. It's learning curve can be a bit steep, but after you have learned at least the basics you should be quite safe in most every unix-environment you encounter.

Now that I'm back on the track you should next try to restart your postgresql database. On a Centos machine this I believe is done by:

sudo service postgresql restart

Now you should be able to access your Centos-machines postrgresql database from 10.0.2.12

share|improve this answer
1  
The sudo in the last command won't work since the priviledged operation is done by a redirection, which is handled by the shell, not by the program you run with sudo (echo, in this case). –  bortzmeyer Sep 11 '09 at 12:00
    
Good point! It should be written as sudo "echo 'host all all 10.0.2.12 255.255.255.255 trust' >> /var/lib/pgsql/data/pg_hba.conf" –  pyhimys Sep 12 '09 at 15:12
    
The ideal way would be to put it under version control in an admin location and deploy it after changing it via SSH etc –  vfclists Jun 23 '12 at 16:50
add comment

Don't forget to edit postgresql.conf and change/add line

listen_addresses = '*'

Postgresql by default listens on localhost

share|improve this answer
    
Isn't your list_addresses example too permissive? That is a gaping security hole if the server runs on multiple IPs, ie public, internal, VPN etc. –  vfclists Jun 23 '12 at 16:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.