CYBERTEC Logo

pg_service.conf: The forgotten config file

02.2016 / Category: / Tags: |

Once in a while, I rediscover features in PostgreSQL which have been there for many many years, but which are so rarely used that people tend not to talk about them. One of those (in my experience) rarely-used features is the ability to create a file called "pg_service.conf".

The basic idea is to configure services in a config file and address them without having to worry about host, port, user and so on.

A simple pg_service.conf file

How does it work? Well, here's an example:

In this case two services have been defined. Note that PostgreSQL uses a standard .ini-file format. Inside a section you can use all connection parameters available in PostgreSQL (a full list can be found here: http://www.postgresql.org/docs/9.5/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS).

The first important thing is that a service can be referenced by name. The way to do that is to set an environment variable:

 

A connection can now be established without passing parameters to psql:

 

Actually a pg_service.conf file is a pretty convenient thing to have. Maybe I should reconsider my behavior, and use this thing more often.

In case you need any assistance, please feel free to contact us.
 


In order to receive regular updates on important changes in PostgreSQL, subscribe to our newsletter, or follow us on Twitter, Facebook, or LinkedIn.

0 0 votes
Article Rating
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
pabloedison
pabloedison
7 years ago

Also, instead of export PGSERVICE you can swap services at psql invocation time in one line with PGSERVICE=myservice psql

pabloedison
pabloedison
7 years ago
Reply to  pabloedison

Even better, I came up with adding this to my .bashrc:


pg() { PGSERVICE=$1 psql

}

random blogger
random blogger
3 years ago

you can also do "psql service=hansservice" because libpq accepts a connection string in the database name parameter.

CYBERTEC Logo white
CYBERTEC PostgreSQL International GmbH
Römerstraße 19
2752 Wöllersdorf
Austria

+43 (0) 2622 93022-0
office@cybertec.at

Get the newest PostgreSQL Info & Tools


    This site is protected by reCAPTCHA and the Google Privacy Policy & Terms of Service apply.

    ©
    2024
    CYBERTEC PostgreSQL International GmbH
    phone-handsetmagnifiercrosscross-circle
    3
    0
    Would love your thoughts, please comment.x
    ()
    x
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram