1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>32.17. The Connection Service File</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="libpq-pgpass.html" title="32.16. The Password File" /><link rel="next" href="libpq-ldap.html" title="32.18. LDAP Lookup of Connection Parameters" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">32.17. The Connection Service File</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="libpq-pgpass.html" title="32.16. The Password File">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="libpq.html" title="Chapter 32. libpq — C Library">Up</a></td><th width="60%" align="center">Chapter 32. <span class="application">libpq</span> — C Library</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="libpq-ldap.html" title="32.18. LDAP Lookup of Connection Parameters">Next</a></td></tr></table><hr /></div><div class="sect1" id="LIBPQ-PGSERVICE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">32.17. The Connection Service File <a href="#LIBPQ-PGSERVICE" class="id_link">#</a></h2></div></div></div><a id="id-1.7.3.24.2" class="indexterm"></a><a id="id-1.7.3.24.3" class="indexterm"></a><a id="id-1.7.3.24.4" class="indexterm"></a><p>
3 The connection service file allows libpq connection parameters to be
4 associated with a single service name. That service name can then be
5 specified using the <code class="literal">service</code> key word
6 in a libpq connection string, and the associated settings will be
7 used. This allows connection parameters to be modified without requiring
8 a recompile of the libpq-using application. The service name can also be
9 specified using the <code class="envar">PGSERVICE</code> environment variable.
11 Service names can be defined in either a per-user service file or a
12 system-wide file. If the same service name exists in both the user
13 and the system file, the user file takes precedence.
14 By default, the per-user service file is named
15 <code class="filename">~/.pg_service.conf</code>.
16 On Microsoft Windows, it is named
17 <code class="filename">%APPDATA%\postgresql\.pg_service.conf</code> (where
18 <code class="filename">%APPDATA%</code> refers to the Application Data subdirectory
19 in the user's profile). A different file name can be specified by
20 setting the environment variable <code class="envar">PGSERVICEFILE</code>.
21 The system-wide file is named <code class="filename">pg_service.conf</code>.
22 By default it is sought in the <code class="filename">etc</code> directory
23 of the <span class="productname">PostgreSQL</span> installation
24 (use <code class="literal">pg_config --sysconfdir</code> to identify this
25 directory precisely). Another directory, but not a different file
26 name, can be specified by setting the environment variable
27 <code class="envar">PGSYSCONFDIR</code>.
29 Either service file uses an <span class="quote">“<span class="quote">INI file</span>”</span> format where the section
30 name is the service name and the parameters are connection
31 parameters; see <a class="xref" href="libpq-connect.html#LIBPQ-PARAMKEYWORDS" title="32.1.2. Parameter Key Words">Section 32.1.2</a> for a list. For
33 </p><pre class="programlisting">
40 An example file is provided in
41 the <span class="productname">PostgreSQL</span> installation at
42 <code class="filename">share/pg_service.conf.sample</code>.
44 Connection parameters obtained from a service file are combined with
45 parameters obtained from other sources. A service file setting
46 overrides the corresponding environment variable, and in turn can be
47 overridden by a value given directly in the connection string.
48 For example, using the above service file, a connection string
49 <code class="literal">service=mydb port=5434</code> will use
50 host <code class="literal">somehost</code>, port <code class="literal">5434</code>,
51 user <code class="literal">admin</code>, and other parameters as set by
52 environment variables or built-in defaults.
53 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="libpq-pgpass.html" title="32.16. The Password File">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="libpq.html" title="Chapter 32. libpq — C Library">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="libpq-ldap.html" title="32.18. LDAP Lookup of Connection Parameters">Next</a></td></tr><tr><td width="40%" align="left" valign="top">32.16. The Password File </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 32.18. LDAP Lookup of Connection Parameters</td></tr></table></div></body></html>