Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I would like to obtain the path to pg_hba.conf from the shell. The path varies between versions of PostgreSQL. For instance, for 8.4 and 9.1:

/etc/postgresql/8.4/main/pg_hba.conf
/etc/postgresql/9.1/main/pg_hba.conf

I have tried the pg_config command, but it does not seem to include this information.

This is so that I can use a uniform command for opening pg_hba.conf and other PostgreSQL configuration files for editing.

I'm using bash.

share|improve this question
    
How about locate pg_hba.conf ? –  Pramod Nov 14 '13 at 17:48
    
@Pramod There could tens of them lying around. What's if you want to know which one of them is being used by Postgres. –  Bleeding Fingers Feb 27 at 9:03

3 Answers 3

up vote 13 down vote accepted

pg_config is for compliation information, to help extensions and client programs compile and link against PostgreSQL. It knows nothing about the active PostgreSQL instance(s) on the machine, only the binaries.

pg_hba.conf can appear in many other places depending on how Pg was installed. The standard location is pg_hba.conf within the data_directory of the database (which could be in /home, /var/lib/pgsql, /var/lib/postgresql/[version]/, /opt/postgres/, etc etc etc) but users and packagers can put it wherever they like. Unfortunately.

The only valid ways find pg_hba.conf is to ask a running PostgreSQL instance where it's pg_hba.conf is, or ask the sysadmin where it is. You can't even rely on asking where the datadir is and parsing postgresql.conf because an init script might passed a param like -c hba_file=/some/other/path when starting Pg.

What you want to do is ask PostgreSQL:

SHOW hba_file;

This command must be run on a superuser session, so for shell scripting you might write something like:

psql -t -P format=unaligned -c 'show hba_file';

and set the environment variables PGUSER, PGDATABASE, etc to ensure that the connection is right.

Yes, this is somewhat of a chicken-and-egg problem, in that if the user can't connect (say, after screwing up editing pg_hba.conf) you can't find pg_hba.conf in order to fix it.

If you're targeting specifically Ubuntu systems with PostgreSQL installed from Debian/Ubuntu packages it gets a little easier. You don't have to deal with hand-compiled-from-source Pg that someone's initdb'd a datadir for in their home dir, or an EnterpriseDB Pg install in /opt, etc. You can ask pg_wrapper, the Debian/Ubuntu multi-version Pg manager, where PostgreSQL is using the pg_lsclusters command from pg_wrapper.

share|improve this answer
    
This might not be valid for 9.3, I get: psql: invalid port number: "format=unaligned" when I run that psql command. –  jcollum Jun 25 at 15:39
    
@jcollum Nope, just a typo. -P not -p. Fixed. –  Craig Ringer Jun 26 at 3:40

This is how I do it:

john@host:~$ ls /etc/postgresql
9.1
john@host:~$ sudo vim /etc/postgresql/9.1/main/pg_hba.conf

Since the path is like this: /etc/postgresql/[VERSION]/main/pg_hba.conf

share|improve this answer

To know where the file is simply type:

pg_lsclusters
share|improve this answer
    
If you want to update the pg_hba.conf file then issue the following command: $ sudo nano /etc/postgresql/9.1/main/pg_hba.conf –  nirvanastack Oct 14 '13 at 2:27
    
-bash: pg_lsclusters: command not found –  Mona Jalal Aug 17 at 20:14

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.