CYBERTEC PostgreSQL Logo

pg_service.conf: The forgotten config file

02.2016 / Category: / Tags:

Occasionally, I uncover long-standing but overlooked PostgreSQL features. One such rarely-used feature is "pg_service.conf," a file you can create to configure services. It simplifies access by eliminating the need to specify host, port, and user details.

A simple pg_service.conf file

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

[hansservice]

host=localhost port=5432 dbname=test user=hs password=abc

[paulservice]

host=192.168.0.45 port=5432 dbname=xyz user=paul password=cde

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 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
8 years ago

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

pabloedison
pabloedison
8 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
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram