1 <!-- doc/src/sgml/runtime.sgml -->
4 <title>Server Setup and Operation
</title>
7 This chapter discusses how to set up and run the database server,
8 and its interactions with the operating system.
12 The directions in this chapter assume that you are working with
13 plain
<productname>PostgreSQL
</productname> without any additional
14 infrastructure, for example a copy that you built from source
15 according to the directions in the preceding chapters.
16 If you are working with a pre-packaged or vendor-supplied
17 version of
<productname>PostgreSQL
</productname>, it is likely that
18 the packager has made special provisions for installing and starting
19 the database server according to your system's conventions.
20 Consult the package-level documentation for details.
23 <sect1 id=
"postgres-user">
24 <title>The
<productname>PostgreSQL
</productname> User Account
</title>
27 <primary>postgres user
</primary>
31 As with any server daemon that is accessible to the outside world,
32 it is advisable to run
<productname>PostgreSQL
</productname> under a
33 separate user account. This user account should only own the data
34 that is managed by the server, and should not be shared with other
35 daemons. (For example, using the user
<literal>nobody
</literal> is a bad
36 idea.) In particular, it is advisable that this user account not own
37 the
<productname>PostgreSQL
</productname> executable files, to ensure
38 that a compromised server process could not modify those executables.
42 Pre-packaged versions of
<productname>PostgreSQL
</productname> will
43 typically create a suitable user account automatically during
48 To add a Unix user account to your system, look for a command
49 <command>useradd
</command> or
<command>adduser
</command>. The user
50 name
<systemitem>postgres
</systemitem> is often used, and is assumed
51 throughout this book, but you can use another name if you like.
55 <sect1 id=
"creating-cluster">
56 <title>Creating a Database Cluster
</title>
59 <primary>database cluster
</primary>
63 <primary>data area
</primary>
64 <see>database cluster
</see>
68 Before you can do anything, you must initialize a database storage
69 area on disk. We call this a
<firstterm>database cluster
</firstterm>.
70 (The
<acronym>SQL
</acronym> standard uses the term catalog cluster.) A
71 database cluster is a collection of databases that is managed by a
72 single instance of a running database server. After initialization, a
73 database cluster will contain a database named
<literal>postgres
</literal>,
74 which is meant as a default database for use by utilities, users and third
75 party applications. The database server itself does not require the
76 <literal>postgres
</literal> database to exist, but many external utility
77 programs assume it exists. There are two more databases created within
78 each cluster during initialization, named
<literal>template1
</literal>
79 and
<literal>template0
</literal>. As the names suggest, these will be
80 used as templates for subsequently-created databases; they should not be
81 used for actual work. (See
<xref linkend=
"managing-databases"/> for
82 information about creating new databases within a cluster.)
86 In file system terms, a database cluster is a single directory
87 under which all data will be stored. We call this the
<firstterm>data
88 directory
</firstterm> or
<firstterm>data area
</firstterm>. It is
89 completely up to you where you choose to store your data. There is no
90 default, although locations such as
91 <filename>/usr/local/pgsql/data
</filename> or
92 <filename>/var/lib/pgsql/data
</filename> are popular.
93 The data directory must be initialized before being used, using the program
94 <xref linkend=
"app-initdb"/><indexterm><primary>initdb
</primary></indexterm>
95 which is installed with
<productname>PostgreSQL
</productname>.
99 If you are using a pre-packaged version
100 of
<productname>PostgreSQL
</productname>, it may well have a specific
101 convention for where to place the data directory, and it may also
102 provide a script for creating the data directory. In that case you
103 should use that script in preference to
104 running
<command>initdb
</command> directly.
105 Consult the package-level documentation for details.
109 To initialize a database cluster manually,
110 run
<command>initdb
</command> and specify the desired
111 file system location of the database cluster with the
112 <option>-D
</option> option, for example:
114 <prompt>$
</prompt> <userinput>initdb -D /usr/local/pgsql/data
</userinput>
116 Note that you must execute this command while logged into the
117 <productname>PostgreSQL
</productname> user account, which is
118 described in the previous section.
123 As an alternative to the
<option>-D
</option> option, you can set
124 the environment variable
<envar>PGDATA
</envar>.
125 <indexterm><primary><envar>PGDATA
</envar></primary></indexterm>
130 Alternatively, you can run
<command>initdb
</command> via
131 the
<xref linkend=
"app-pg-ctl"/>
132 program
<indexterm><primary>pg_ctl
</primary></indexterm> like so:
134 <prompt>$
</prompt> <userinput>pg_ctl -D /usr/local/pgsql/data initdb
</userinput>
136 This may be more intuitive if you are
137 using
<command>pg_ctl
</command> for starting and stopping the
138 server (see
<xref linkend=
"server-start"/>), so
139 that
<command>pg_ctl
</command> would be the sole command you use
140 for managing the database server instance.
144 <command>initdb
</command> will attempt to create the directory you
145 specify if it does not already exist. Of course, this will fail if
146 <command>initdb
</command> does not have permissions to write in the
147 parent directory. It's generally recommendable that the
148 <productname>PostgreSQL
</productname> user own not just the data
149 directory but its parent directory as well, so that this should not
150 be a problem. If the desired parent directory doesn't exist either,
151 you will need to create it first, using root privileges if the
152 grandparent directory isn't writable. So the process might look
155 root#
<userinput>mkdir /usr/local/pgsql
</userinput>
156 root#
<userinput>chown postgres /usr/local/pgsql
</userinput>
157 root#
<userinput>su postgres
</userinput>
158 postgres$
<userinput>initdb -D /usr/local/pgsql/data
</userinput>
163 <command>initdb
</command> will refuse to run if the data directory
164 exists and already contains files; this is to prevent accidentally
165 overwriting an existing installation.
169 Because the data directory contains all the data stored in the
170 database, it is essential that it be secured from unauthorized
171 access.
<command>initdb
</command> therefore revokes access
172 permissions from everyone but the
173 <productname>PostgreSQL
</productname> user, and optionally, group.
174 Group access, when enabled, is read-only. This allows an unprivileged
175 user in the same group as the cluster owner to take a backup of the
176 cluster data or perform other operations that only require read access.
180 Note that enabling or disabling group access on an existing cluster requires
181 the cluster to be shut down and the appropriate mode to be set on all
182 directories and files before restarting
183 <productname>PostgreSQL
</productname>. Otherwise, a mix of modes might
184 exist in the data directory. For clusters that allow access only by the
185 owner, the appropriate modes are
<literal>0700</literal> for directories
186 and
<literal>0600</literal> for files. For clusters that also allow
187 reads by the group, the appropriate modes are
<literal>0750</literal>
188 for directories and
<literal>0640</literal> for files.
192 However, while the directory contents are secure, the default
193 client authentication setup allows any local user to connect to the
194 database and even become the database superuser. If you do not
195 trust other local users, we recommend you use one of
196 <command>initdb
</command>'s
<option>-W
</option>,
<option>--pwprompt
</option>
197 or
<option>--pwfile
</option> options to assign a password to the
198 database superuser.
<indexterm>
199 <primary>password
</primary>
200 <secondary>of the superuser
</secondary>
202 Also, specify
<option>-A scram-sha-
256</option>
203 so that the default
<literal>trust
</literal> authentication
204 mode is not used; or modify the generated
<filename>pg_hba.conf
</filename>
205 file after running
<command>initdb
</command>, but
206 <emphasis>before
</emphasis> you start the server for the first time. (Other
207 reasonable approaches include using
<literal>peer
</literal> authentication
208 or file system permissions to restrict connections. See
<xref
209 linkend=
"client-authentication"/> for more information.)
213 <command>initdb
</command> also initializes the default
214 locale
<indexterm><primary>locale
</primary></indexterm> for the database cluster.
215 Normally, it will just take the locale settings in the environment
216 and apply them to the initialized database. It is possible to
217 specify a different locale for the database; more information about
218 that can be found in
<xref linkend=
"locale"/>. The default sort order used
219 within the particular database cluster is set by
220 <command>initdb
</command>, and while you can create new databases using
221 different sort order, the order used in the template databases that initdb
222 creates cannot be changed without dropping and recreating them.
223 There is also a performance impact for using locales
224 other than
<literal>C
</literal> or
<literal>POSIX
</literal>. Therefore, it is
225 important to make this choice correctly the first time.
229 <command>initdb
</command> also sets the default character set encoding
230 for the database cluster. Normally this should be chosen to match the
231 locale setting. For details see
<xref linkend=
"multibyte"/>.
235 Non-
<literal>C
</literal> and non-
<literal>POSIX
</literal> locales rely on the
236 operating system's collation library for character set ordering.
237 This controls the ordering of keys stored in indexes. For this reason,
238 a cluster cannot switch to an incompatible collation library version,
239 either through snapshot restore, binary streaming replication, a
240 different operating system, or an operating system upgrade.
243 <sect2 id=
"creating-cluster-mount-points">
244 <title>Use of Secondary File Systems
</title>
246 <indexterm zone=
"creating-cluster-mount-points">
247 <primary>file system mount points
</primary>
251 Many installations create their database clusters on file systems
252 (volumes) other than the machine's
<quote>root
</quote> volume. If you
253 choose to do this, it is not advisable to try to use the secondary
254 volume's topmost directory (mount point) as the data directory.
255 Best practice is to create a directory within the mount-point
256 directory that is owned by the
<productname>PostgreSQL
</productname>
257 user, and then create the data directory within that. This avoids
258 permissions problems, particularly for operations such
259 as
<application>pg_upgrade
</application>, and it also ensures clean failures if
260 the secondary volume is taken offline.
265 <sect2 id=
"creating-cluster-filesystem">
266 <title>File Systems
</title>
269 Generally, any file system with POSIX semantics can be used for
270 PostgreSQL. Users prefer different file systems for a variety of reasons,
271 including vendor support, performance, and familiarity. Experience
272 suggests that, all other things being equal, one should not expect major
273 performance or behavior changes merely from switching file systems or
274 making minor file system configuration changes.
277 <sect3 id=
"creating-cluster-nfs">
280 <indexterm zone=
"creating-cluster-nfs">
281 <primary>NFS
</primary>
285 It is possible to use an
<acronym>NFS
</acronym> file system for storing
286 the
<productname>PostgreSQL
</productname> data directory.
287 <productname>PostgreSQL
</productname> does nothing special for
288 <acronym>NFS
</acronym> file systems, meaning it assumes
289 <acronym>NFS
</acronym> behaves exactly like locally-connected drives.
290 <productname>PostgreSQL
</productname> does not use any functionality that
291 is known to have nonstandard behavior on
<acronym>NFS
</acronym>, such as
296 The only firm requirement for using
<acronym>NFS
</acronym> with
297 <productname>PostgreSQL
</productname> is that the file system is mounted
298 using the
<literal>hard
</literal> option. With the
299 <literal>hard
</literal> option, processes can
<quote>hang
</quote>
300 indefinitely if there are network problems, so this configuration will
301 require a careful monitoring setup. The
<literal>soft
</literal> option
302 will interrupt system calls in case of network problems, but
303 <productname>PostgreSQL
</productname> will not repeat system calls
304 interrupted in this way, so any such interruption will result in an I/O
305 error being reported.
309 It is not necessary to use the
<literal>sync
</literal> mount option. The
310 behavior of the
<literal>async
</literal> option is sufficient, since
311 <productname>PostgreSQL
</productname> issues
<literal>fsync
</literal>
312 calls at appropriate times to flush the write caches. (This is analogous
313 to how it works on a local file system.) However, it is strongly
314 recommended to use the
<literal>sync
</literal> export option on the NFS
315 <emphasis>server
</emphasis> on systems where it exists (mainly Linux).
316 Otherwise, an
<literal>fsync
</literal> or equivalent on the NFS client is
317 not actually guaranteed to reach permanent storage on the server, which
318 could cause corruption similar to running with the parameter
<xref
319 linkend=
"guc-fsync"/> off. The defaults of these mount and export
320 options differ between vendors and versions, so it is recommended to
321 check and perhaps specify them explicitly in any case to avoid any
326 In some cases, an external storage product can be accessed either via NFS
327 or a lower-level protocol such as iSCSI. In the latter case, the storage
328 appears as a block device and any available file system can be created on
329 it. That approach might relieve the DBA from having to deal with some of
330 the idiosyncrasies of NFS, but of course the complexity of managing
331 remote storage then happens at other levels.
338 <sect1 id=
"server-start">
339 <title>Starting the Database Server
</title>
342 Before anyone can access the database, you must start the database
343 server. The database server program is called
344 <command>postgres
</command>.
<indexterm><primary>postgres
</primary></indexterm>
348 If you are using a pre-packaged version
349 of
<productname>PostgreSQL
</productname>, it almost certainly includes
350 provisions for running the server as a background task according to the
351 conventions of your operating system. Using the package's
352 infrastructure to start the server will be much less work than figuring
353 out how to do this yourself. Consult the package-level documentation
358 The bare-bones way to start the server manually is just to invoke
359 <command>postgres
</command> directly, specifying the location of the
360 data directory with the
<option>-D
</option> option, for example:
362 $
<userinput>postgres -D /usr/local/pgsql/data
</userinput>
364 which will leave the server running in the foreground. This must be
365 done while logged into the
<productname>PostgreSQL
</productname> user
366 account. Without
<option>-D
</option>, the server will try to use
367 the data directory named by the environment variable
<envar>PGDATA
</envar>.
368 If that variable is not provided either, it will fail.
372 Normally it is better to start
<command>postgres
</command> in the
373 background. For this, use the usual Unix shell syntax:
375 $
<userinput>postgres -D /usr/local/pgsql/data
>logfile
2>&1 &</userinput>
377 It is important to store the server's
<systemitem>stdout
</systemitem> and
378 <systemitem>stderr
</systemitem> output somewhere, as shown above. It will help
379 for auditing purposes and to diagnose problems. (See
<xref
380 linkend=
"logfile-maintenance"/> for a more thorough discussion of log
385 The
<command>postgres
</command> program also takes a number of other
386 command-line options. For more information, see the
387 <xref linkend=
"app-postgres"/> reference page
388 and
<xref linkend=
"runtime-config"/> below.
392 This shell syntax can get tedious quickly. Therefore the wrapper
394 <xref linkend=
"app-pg-ctl"/><indexterm><primary>pg_ctl
</primary></indexterm>
395 is provided to simplify some tasks. For example:
397 pg_ctl start -l logfile
399 will start the server in the background and put the output into the
400 named log file. The
<option>-D
</option> option has the same meaning
401 here as for
<command>postgres
</command>.
<command>pg_ctl
</command>
402 is also capable of stopping the server.
406 Normally, you will want to start the database server when the
407 computer boots.
<indexterm>
408 <primary>booting
</primary>
409 <secondary>starting the server during
</secondary>
411 Autostart scripts are operating-system-specific.
412 There are a few example scripts distributed with
413 <productname>PostgreSQL
</productname> in the
414 <filename>contrib/start-scripts
</filename> directory. Installing one will require
419 Different systems have different conventions for starting up daemons
420 at boot time. Many systems have a file
421 <filename>/etc/rc.local
</filename> or
422 <filename>/etc/rc.d/rc.local
</filename>. Others use
<filename>init.d
</filename> or
423 <filename>rc.d
</filename> directories. Whatever you do, the server must be
424 run by the
<productname>PostgreSQL
</productname> user account
425 <emphasis>and not by root
</emphasis> or any other user. Therefore you
426 probably should form your commands using
427 <literal>su postgres -c '...'
</literal>. For example:
429 su postgres -c 'pg_ctl start -D /usr/local/pgsql/data -l serverlog'
434 Here are a few more operating-system-specific suggestions. (In each
435 case be sure to use the proper installation directory and user
436 name where we show generic values.)
441 For
<productname>FreeBSD
</productname>, look at the file
442 <filename>contrib/start-scripts/freebsd
</filename> in the
443 <productname>PostgreSQL
</productname> source distribution.
444 <indexterm><primary>FreeBSD
</primary><secondary>start script
</secondary></indexterm>
450 On
<productname>OpenBSD
</productname>, add the following lines
451 to the file
<filename>/etc/rc.local
</filename>:
452 <indexterm><primary>OpenBSD
</primary><secondary>start script
</secondary></indexterm>
454 if [ -x /usr/local/pgsql/bin/pg_ctl -a -x /usr/local/pgsql/bin/postgres ]; then
455 su -l postgres -c '/usr/local/pgsql/bin/pg_ctl start -s -l /var/postgresql/log -D /usr/local/pgsql/data'
456 echo -n ' postgresql'
464 On
<productname>Linux
</productname> systems either add
465 <indexterm><primary>Linux
</primary><secondary>start script
</secondary></indexterm>
467 /usr/local/pgsql/bin/pg_ctl start -l logfile -D /usr/local/pgsql/data
469 to
<filename>/etc/rc.d/rc.local
</filename>
470 or
<filename>/etc/rc.local
</filename> or look at the file
471 <filename>contrib/start-scripts/linux
</filename> in the
472 <productname>PostgreSQL
</productname> source distribution.
476 When using
<application>systemd
</application>, you can use the following
477 service unit file (e.g.,
478 at
<filename>/etc/systemd/system/postgresql.service
</filename>):
<indexterm><primary>systemd
</primary></indexterm>
481 Description=PostgreSQL database server
482 Documentation=man:postgres(
1)
483 After=network-online.target
484 Wants=network-online.target
489 ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
490 ExecReload=/bin/kill -HUP $MAINPID
496 WantedBy=multi-user.target
498 Using
<literal>Type=notify
</literal> requires that the server binary was
499 built with
<literal>configure --with-systemd
</literal>.
503 Consider carefully the timeout
504 setting.
<application>systemd
</application> has a default timeout of
90
505 seconds as of this writing and will kill a process that does not report
506 readiness within that time. But a
<productname>PostgreSQL
</productname>
507 server that might have to perform crash recovery at startup could take
508 much longer to become ready. The suggested value
509 of
<literal>infinity
</literal> disables the timeout logic.
515 On
<productname>NetBSD
</productname>, use either the
516 <productname>FreeBSD
</productname> or
517 <productname>Linux
</productname> start scripts, depending on
519 <indexterm><primary>NetBSD
</primary><secondary>start script
</secondary></indexterm>
525 On
<productname>Solaris
</productname>, create a file called
526 <filename>/etc/init.d/postgresql
</filename> that contains
528 <indexterm><primary>Solaris
</primary><secondary>start script
</secondary></indexterm>
530 su - postgres -c
"/usr/local/pgsql/bin/pg_ctl start -l logfile -D /usr/local/pgsql/data"
532 Then, create a symbolic link to it in
<filename>/etc/rc3.d
</filename> as
533 <filename>S99postgresql
</filename>.
541 While the server is running, its
542 <acronym>PID
</acronym> is stored in the file
543 <filename>postmaster.pid
</filename> in the data directory. This is
544 used to prevent multiple server instances from
545 running in the same data directory and can also be used for
546 shutting down the server.
549 <sect2 id=
"server-start-failures">
550 <title>Server Start-up Failures
</title>
553 There are several common reasons the server might fail to
554 start. Check the server's log file, or start it by hand (without
555 redirecting standard output or standard error) and see what error
556 messages appear. Below we explain some of the most common error
557 messages in more detail.
562 LOG: could not bind IPv4 address
"127.0.0.1": Address already in use
563 HINT: Is another postmaster already running on port
5432? If not, wait a few seconds and retry.
564 FATAL: could not create any TCP/IP sockets
566 This usually means just what it suggests: you tried to start
567 another server on the same port where one is already running.
568 However, if the kernel error message is not
<computeroutput>Address
569 already in use
</computeroutput> or some variant of that, there might
570 be a different problem. For example, trying to start a server
571 on a reserved port number might draw something like:
573 $
<userinput>postgres -p
666</userinput>
574 LOG: could not bind IPv4 address
"127.0.0.1": Permission denied
575 HINT: Is another postmaster already running on port
666? If not, wait a few seconds and retry.
576 FATAL: could not create any TCP/IP sockets
583 FATAL: could not create shared memory segment: Invalid argument
584 DETAIL: Failed system call was shmget(key=
5440001, size=
4011376640,
03600).
586 probably means your kernel's limit on the size of shared memory is
587 smaller than the work area
<productname>PostgreSQL
</productname>
588 is trying to create (
4011376640 bytes in this example).
589 This is only likely to happen if you have set
<literal>shared_memory_type
</literal>
590 to
<literal>sysv
</literal>. In that case, you
591 can try starting the server with a smaller-than-normal number of
592 buffers (
<xref linkend=
"guc-shared-buffers"/>), or
593 reconfigure your kernel to increase the allowed shared memory
594 size. You might also see this message when trying to start multiple
595 servers on the same machine, if their total space requested
596 exceeds the kernel limit.
602 FATAL: could not create semaphores: No space left on device
603 DETAIL: Failed system call was semget(
5440126,
17,
03600).
605 does
<emphasis>not
</emphasis> mean you've run out of disk
606 space. It means your kernel's limit on the number of
<systemitem
607 class=
"osname">System V
</systemitem> semaphores is smaller than the number
608 <productname>PostgreSQL
</productname> wants to create. As above,
609 you might be able to work around the problem by starting the
610 server with a reduced number of allowed connections
611 (
<xref linkend=
"guc-max-connections"/>), but you'll eventually want to
612 increase the kernel limit.
616 Details about configuring
<systemitem class=
"osname">System V
</systemitem>
617 <acronym>IPC
</acronym> facilities are given in
<xref linkend=
"sysvipc"/>.
621 <sect2 id=
"client-connection-problems">
622 <title>Client Connection Problems
</title>
625 Although the error conditions possible on the client side are quite
626 varied and application-dependent, a few of them might be directly
627 related to how the server was started. Conditions other than
628 those shown below should be documented with the respective client
634 psql: error: connection to server at
"server.joe.com" (
123.123.123.123), port
5432 failed: Connection refused
635 Is the server running on that host and accepting TCP/IP connections?
637 This is the generic
<quote>I couldn't find a server to talk
638 to
</quote> failure. It looks like the above when TCP/IP
639 communication is attempted. A common mistake is to forget to
640 configure
<xref linkend=
"guc-listen-addresses"/> so that the server
641 accepts remote TCP connections.
645 Alternatively, you might get this when attempting Unix-domain socket
646 communication to a local server:
648 psql: error: connection to server on socket
"/tmp/.s.PGSQL.5432" failed: No such file or directory
649 Is the server running locally and accepting connections on that socket?
651 If the server is indeed running, check that the client's idea of the
652 socket path (here
<literal>/tmp
</literal>) agrees with the server's
653 <xref linkend=
"guc-unix-socket-directories"/> setting.
657 A connection failure message always shows the server address or socket
658 path name, which is useful in verifying that the client is trying to
659 connect to the right place. If there is in fact no server
660 listening there, the kernel error message will typically be either
661 <computeroutput>Connection refused
</computeroutput> or
662 <computeroutput>No such file or directory
</computeroutput>, as
663 illustrated. (It is important to realize that
664 <computeroutput>Connection refused
</computeroutput> in this context
665 does
<emphasis>not
</emphasis> mean that the server got your
666 connection request and rejected it. That case will produce a
667 different message, as shown in
<xref
668 linkend=
"client-authentication-problems"/>.) Other error messages
669 such as
<computeroutput>Connection timed out
</computeroutput> might
670 indicate more fundamental problems, like lack of network
671 connectivity, or a firewall blocking the connection.
676 <sect1 id=
"kernel-resources">
677 <title>Managing Kernel Resources
</title>
680 <productname>PostgreSQL
</productname> can sometimes exhaust various operating system
681 resource limits, especially when multiple copies of the server are running
682 on the same system, or in very large installations. This section explains
683 the kernel resources used by
<productname>PostgreSQL
</productname> and the steps you
684 can take to resolve problems related to kernel resource consumption.
688 <title>Shared Memory and Semaphores
</title>
690 <indexterm zone=
"sysvipc">
691 <primary>shared memory
</primary>
694 <indexterm zone=
"sysvipc">
695 <primary>semaphores
</primary>
699 <productname>PostgreSQL
</productname> requires the operating system to provide
700 inter-process communication (
<acronym>IPC
</acronym>) features, specifically
701 shared memory and semaphores. Unix-derived systems typically provide
702 <quote><systemitem class=
"osname">System V
</systemitem></quote> <acronym>IPC
</acronym>,
703 <quote><systemitem class=
"osname">POSIX
</systemitem></quote> <acronym>IPC
</acronym>, or both.
704 <systemitem class=
"osname">Windows
</systemitem> has its own implementation of
705 these features and is not discussed here.
709 By default,
<productname>PostgreSQL
</productname> allocates
710 a very small amount of System V shared memory, as well as a much larger
711 amount of anonymous
<function>mmap
</function> shared memory.
712 Alternatively, a single large System V shared memory region can be used
713 (see
<xref linkend=
"guc-shared-memory-type"/>).
715 In addition a significant number of semaphores, which can be either
716 System V or POSIX style, are created at server startup. Currently,
717 POSIX semaphores are used on Linux and FreeBSD systems while other
718 platforms use System V semaphores.
722 System V
<acronym>IPC
</acronym> features are typically constrained by
723 system-wide allocation limits.
724 When
<productname>PostgreSQL
</productname> exceeds one of these limits,
725 the server will refuse to start and
726 should leave an instructive error message describing the problem
727 and what to do about it. (See also
<xref
728 linkend=
"server-start-failures"/>.) The relevant kernel
729 parameters are named consistently across different systems;
<xref
730 linkend=
"sysvipc-parameters"/> gives an overview. The methods to set
731 them, however, vary. Suggestions for some platforms are given below.
734 <table id=
"sysvipc-parameters">
735 <title><systemitem class=
"osname">System V
</systemitem> <acronym>IPC
</acronym> Parameters
</title>
738 <colspec colname=
"col1" colwidth=
"1*"/>
739 <colspec colname=
"col2" colwidth=
"3*"/>
740 <colspec colname=
"col3" colwidth=
"3*"/>
744 <entry>Description
</entry>
745 <entry>Values needed to run one
<productname>PostgreSQL
</productname> instance
</entry>
751 <entry><varname>SHMMAX
</varname></entry>
752 <entry>Maximum size of shared memory segment (bytes)
</entry>
753 <entry>at least
1kB, but the default is usually much higher
</entry>
757 <entry><varname>SHMMIN
</varname></entry>
758 <entry>Minimum size of shared memory segment (bytes)
</entry>
763 <entry><varname>SHMALL
</varname></entry>
764 <entry>Total amount of shared memory available (bytes or pages)
</entry>
765 <entry>same as
<varname>SHMMAX
</varname> if bytes,
766 or
<literal>ceil(SHMMAX/PAGE_SIZE)
</literal> if pages,
767 plus room for other applications
</entry>
771 <entry><varname>SHMSEG
</varname></entry>
772 <entry>Maximum number of shared memory segments per process
</entry>
773 <entry>only
1 segment is needed, but the default is much higher
</entry>
777 <entry><varname>SHMMNI
</varname></entry>
778 <entry>Maximum number of shared memory segments system-wide
</entry>
779 <entry>like
<varname>SHMSEG
</varname> plus room for other applications
</entry>
783 <entry><varname>SEMMNI
</varname></entry>
784 <entry>Maximum number of semaphore identifiers (i.e., sets)
</entry>
785 <entry>at least
<literal>ceil(num_os_semaphores /
19)
</literal> plus room for other applications
</entry>
789 <entry><varname>SEMMNS
</varname></entry>
790 <entry>Maximum number of semaphores system-wide
</entry>
791 <entry><literal>ceil(num_os_semaphores /
19) *
20</literal> plus room for other applications
</entry>
795 <entry><varname>SEMMSL
</varname></entry>
796 <entry>Maximum number of semaphores per set
</entry>
797 <entry>at least
20</entry>
801 <entry><varname>SEMMAP
</varname></entry>
802 <entry>Number of entries in semaphore map
</entry>
803 <entry>see text
</entry>
807 <entry><varname>SEMVMX
</varname></entry>
808 <entry>Maximum value of semaphore
</entry>
809 <entry>at least
1000 (The default is often
32767; do not change unless necessary)
</entry>
817 <productname>PostgreSQL
</productname> requires a few bytes of System V shared memory
818 (typically
48 bytes, on
64-bit platforms) for each copy of the server.
819 On most modern operating systems, this amount can easily be allocated.
820 However, if you are running many copies of the server or you explicitly
821 configure the server to use large amounts of System V shared memory (see
822 <xref linkend=
"guc-shared-memory-type"/> and
<xref
823 linkend=
"guc-dynamic-shared-memory-type"/>), it may be necessary to
824 increase
<varname>SHMALL
</varname>, which is the total amount of System V shared
825 memory system-wide. Note that
<varname>SHMALL
</varname> is measured in pages
826 rather than bytes on many systems.
830 Less likely to cause problems is the minimum size for shared
831 memory segments (
<varname>SHMMIN
</varname>), which should be at most
832 approximately
32 bytes for
<productname>PostgreSQL
</productname> (it is
833 usually just
1). The maximum number of segments system-wide
834 (
<varname>SHMMNI
</varname>) or per-process (
<varname>SHMSEG
</varname>) are unlikely
835 to cause a problem unless your system has them set to zero.
839 When using System V semaphores,
840 <productname>PostgreSQL
</productname> uses one semaphore per allowed connection
841 (
<xref linkend=
"guc-max-connections"/>), allowed autovacuum worker process
842 (
<xref linkend=
"guc-autovacuum-worker-slots"/>), allowed WAL sender process
843 (
<xref linkend=
"guc-max-wal-senders"/>), allowed background
844 process (
<xref linkend=
"guc-max-worker-processes"/>), etc., in sets of
19.
845 The runtime-computed parameter
<xref linkend=
"guc-num-os-semaphores"/>
846 reports the number of semaphores required. This parameter can be viewed
847 before starting the server with a
<command>postgres
</command> command like:
849 $
<userinput>postgres -D $PGDATA -C num_os_semaphores
</userinput>
854 Each set of
19 semaphores will
855 also contain a
20th semaphore which contains a
<quote>magic
856 number
</quote>, to detect collision with semaphore sets used by
857 other applications. The maximum number of semaphores in the system
858 is set by
<varname>SEMMNS
</varname>, which consequently must be at least
859 as high as
<literal>num_os_semaphores
</literal> plus one extra for
860 each set of
19 required semaphores (see the formula in
<xref
861 linkend=
"sysvipc-parameters"/>). The parameter
<varname>SEMMNI
</varname>
862 determines the limit on the number of semaphore sets that can
863 exist on the system at one time. Hence this parameter must be at
864 least
<literal>ceil(num_os_semaphores /
19)
</literal>.
866 of allowed connections is a temporary workaround for failures,
867 which are usually confusingly worded
<quote>No space
868 left on device
</quote>, from the function
<function>semget
</function>.
872 In some cases it might also be necessary to increase
873 <varname>SEMMAP
</varname> to be at least on the order of
874 <varname>SEMMNS
</varname>. If the system has this parameter
875 (many do not), it defines the size of the semaphore
876 resource map, in which each contiguous block of available semaphores
877 needs an entry. When a semaphore set is freed it is either added to
878 an existing entry that is adjacent to the freed block or it is
879 registered under a new map entry. If the map is full, the freed
880 semaphores get lost (until reboot). Fragmentation of the semaphore
881 space could over time lead to fewer available semaphores than there
886 Various other settings related to
<quote>semaphore undo
</quote>, such as
887 <varname>SEMMNU
</varname> and
<varname>SEMUME
</varname>, do not affect
888 <productname>PostgreSQL
</productname>.
892 When using POSIX semaphores, the number of semaphores needed is the
893 same as for System V, that is one semaphore per allowed connection
894 (
<xref linkend=
"guc-max-connections"/>), allowed autovacuum worker process
895 (
<xref linkend=
"guc-autovacuum-worker-slots"/>), allowed WAL sender process
896 (
<xref linkend=
"guc-max-wal-senders"/>), allowed background
897 process (
<xref linkend=
"guc-max-worker-processes"/>), etc.
898 On the platforms where this option is preferred, there is no specific
899 kernel limit on the number of POSIX semaphores.
905 <term><systemitem class=
"osname">FreeBSD
</systemitem>
906 <indexterm><primary>FreeBSD
</primary><secondary>IPC configuration
</secondary></indexterm>
910 The default shared memory settings are usually good enough, unless
911 you have set
<literal>shared_memory_type
</literal> to
<literal>sysv
</literal>.
912 System V semaphores are not used on this platform.
916 The default IPC settings can be changed using
917 the
<command>sysctl
</command> or
918 <command>loader
</command> interfaces. The following
919 parameters can be set using
<command>sysctl
</command>:
921 <prompt>#
</prompt> <userinput>sysctl kern.ipc.shmall=
32768</userinput>
922 <prompt>#
</prompt> <userinput>sysctl kern.ipc.shmmax=
134217728</userinput>
924 To make these settings persist over reboots, modify
925 <filename>/etc/sysctl.conf
</filename>.
929 If you have set
<literal>shared_memory_type
</literal> to
930 <literal>sysv
</literal>, you might also want to configure your kernel
931 to lock System V shared memory into RAM and prevent it from being paged
932 out to swap. This can be accomplished using the
<command>sysctl
</command>
933 setting
<literal>kern.ipc.shm_use_phys
</literal>.
937 If running in a FreeBSD jail, you should set its
938 <literal>sysvshm
</literal> parameter to
<literal>new
</literal>, so that
939 it has its own separate System V shared memory namespace.
940 (Before FreeBSD
11.0, it was necessary to enable shared access to
941 the host's IPC namespace from jails, and take measures to avoid
949 <term><systemitem class=
"osname">NetBSD
</systemitem>
950 <indexterm><primary>NetBSD
</primary><secondary>IPC configuration
</secondary></indexterm>
954 The default shared memory settings are usually good enough, unless
955 you have set
<literal>shared_memory_type
</literal> to
<literal>sysv
</literal>.
956 You will usually want to increase
<literal>kern.ipc.semmni
</literal>
957 and
<literal>kern.ipc.semmns
</literal>,
958 as
<systemitem class=
"osname">NetBSD
</systemitem>'s default settings
959 for these are uncomfortably small.
963 IPC parameters can be adjusted using
<command>sysctl
</command>,
966 <prompt>#
</prompt> <userinput>sysctl -w kern.ipc.semmni=
100</userinput>
968 To make these settings persist over reboots, modify
969 <filename>/etc/sysctl.conf
</filename>.
973 If you have set
<literal>shared_memory_type
</literal> to
974 <literal>sysv
</literal>, you might also want to configure your kernel
975 to lock System V shared memory into RAM and prevent it from being paged
976 out to swap. This can be accomplished using the
<command>sysctl
</command>
977 setting
<literal>kern.ipc.shm_use_phys
</literal>.
983 <term><systemitem class=
"osname">OpenBSD
</systemitem>
984 <indexterm><primary>OpenBSD
</primary><secondary>IPC configuration
</secondary></indexterm>
988 The default shared memory settings are usually good enough, unless
989 you have set
<literal>shared_memory_type
</literal> to
<literal>sysv
</literal>.
990 You will usually want to
991 increase
<literal>kern.seminfo.semmni
</literal>
992 and
<literal>kern.seminfo.semmns
</literal>,
993 as
<systemitem class=
"osname">OpenBSD
</systemitem>'s default settings
994 for these are uncomfortably small.
998 IPC parameters can be adjusted using
<command>sysctl
</command>,
1001 <prompt>#
</prompt> <userinput>sysctl kern.seminfo.semmni=
100</userinput>
1003 To make these settings persist over reboots, modify
1004 <filename>/etc/sysctl.conf
</filename>.
1011 <term><systemitem class=
"osname">Linux
</systemitem>
1012 <indexterm><primary>Linux
</primary><secondary>IPC configuration
</secondary></indexterm>
1016 The default shared memory settings are usually good enough, unless
1017 you have set
<literal>shared_memory_type
</literal> to
<literal>sysv
</literal>,
1018 and even then only on older kernel versions that shipped with low defaults.
1019 System V semaphores are not used on this platform.
1023 The shared memory size settings can be changed via the
1024 <command>sysctl
</command> interface. For example, to allow
16 GB:
1026 <prompt>$
</prompt> <userinput>sysctl -w kernel.shmmax=
17179869184</userinput>
1027 <prompt>$
</prompt> <userinput>sysctl -w kernel.shmall=
4194304</userinput>
1029 To make these settings persist over reboots, see
1030 <filename>/etc/sysctl.conf
</filename>.
1038 <term><systemitem class=
"osname">macOS
</systemitem>
1039 <indexterm><primary>macOS
</primary><secondary>IPC configuration
</secondary></indexterm>
1043 The default shared memory and semaphore settings are usually good enough, unless
1044 you have set
<literal>shared_memory_type
</literal> to
<literal>sysv
</literal>.
1047 The recommended method for configuring shared memory in macOS
1048 is to create a file named
<filename>/etc/sysctl.conf
</filename>,
1049 containing variable assignments such as:
1051 kern.sysv.shmmax=
4194304
1055 kern.sysv.shmall=
1024
1057 Note that in some macOS versions,
1058 <emphasis>all five
</emphasis> shared-memory parameters must be set in
1059 <filename>/etc/sysctl.conf
</filename>, else the values will be ignored.
1063 <varname>SHMMAX
</varname> can only be set to a multiple of
4096.
1067 <varname>SHMALL
</varname> is measured in
4 kB pages on this platform.
1071 It is possible to change all but
<varname>SHMMNI
</varname> on the fly, using
1072 <application>sysctl
</application>. But it's still best to set up your preferred
1073 values via
<filename>/etc/sysctl.conf
</filename>, so that the values will be
1074 kept across reboots.
1081 <term><systemitem class=
"osname">Solaris
</systemitem></term>
1082 <term><systemitem class=
"osname">illumos
</systemitem></term>
1085 The default shared memory and semaphore settings are usually good enough for most
1086 <productname>PostgreSQL
</productname> applications. Solaris defaults
1087 to a
<varname>SHMMAX
</varname> of one-quarter of system
<acronym>RAM
</acronym>.
1088 To further adjust this setting, use a project setting associated
1089 with the
<literal>postgres
</literal> user. For example, run the
1090 following as
<literal>root
</literal>:
1092 projadd -c
"PostgreSQL DB User" -K
"project.max-shm-memory=(privileged,8GB,deny)" -U postgres -G postgres user.postgres
1097 This command adds the
<literal>user.postgres
</literal> project and
1098 sets the shared memory maximum for the
<literal>postgres
</literal>
1099 user to
8GB, and takes effect the next time that user logs
1100 in, or when you restart
<productname>PostgreSQL
</productname> (not reload).
1101 The above assumes that
<productname>PostgreSQL
</productname> is run by
1102 the
<literal>postgres
</literal> user in the
<literal>postgres
</literal>
1103 group. No server reboot is required.
1107 Other recommended kernel setting changes for database servers which will
1108 have a large number of connections are:
1110 project.max-shm-ids=(priv,
32768,deny)
1111 project.max-sem-ids=(priv,
4096,deny)
1112 project.max-msg-ids=(priv,
4096,deny)
1117 Additionally, if you are running
<productname>PostgreSQL
</productname>
1118 inside a zone, you may need to raise the zone resource usage
1119 limits as well. See
"Chapter2: Projects and Tasks" in the
1120 <citetitle>System Administrator's Guide
</citetitle> for more
1121 information on
<literal>projects
</literal> and
<command>prctl
</command>.
1130 <sect2 id=
"systemd-removeipc">
1131 <title>systemd RemoveIPC
</title>
1134 <primary>systemd
</primary>
1135 <secondary>RemoveIPC
</secondary>
1139 If
<productname>systemd
</productname> is in use, some care must be taken
1140 that IPC resources (including shared memory) are not prematurely
1141 removed by the operating system. This is especially of concern when
1142 installing PostgreSQL from source. Users of distribution packages of
1143 PostgreSQL are less likely to be affected, as
1144 the
<literal>postgres
</literal> user is then normally created as a system
1149 The setting
<literal>RemoveIPC
</literal>
1150 in
<filename>logind.conf
</filename> controls whether IPC objects are
1151 removed when a user fully logs out. System users are exempt. This
1152 setting defaults to on in stock
<productname>systemd
</productname>, but
1153 some operating system distributions default it to off.
1157 A typical observed effect when this setting is on is that shared memory
1158 objects used for parallel query execution are removed at apparently random
1159 times, leading to errors and warnings while attempting to open and remove
1162 WARNING: could not remove shared memory segment
"/PostgreSQL.1450751626": No such file or directory
1164 Different types of IPC objects (shared memory vs. semaphores, System V
1165 vs. POSIX) are treated slightly differently
1166 by
<productname>systemd
</productname>, so one might observe that some IPC
1167 resources are not removed in the same way as others. But it is not
1168 advisable to rely on these subtle differences.
1172 A
<quote>user logging out
</quote> might happen as part of a maintenance
1173 job or manually when an administrator logs in as
1174 the
<literal>postgres
</literal> user or something similar, so it is hard
1175 to prevent in general.
1179 What is a
<quote>system user
</quote> is determined
1180 at
<productname>systemd
</productname> compile time from
1181 the
<symbol>SYS_UID_MAX
</symbol> setting
1182 in
<filename>/etc/login.defs
</filename>.
1186 Packaging and deployment scripts should be careful to create
1187 the
<literal>postgres
</literal> user as a system user by
1188 using
<literal>useradd -r
</literal>,
<literal>adduser --system
</literal>,
1193 Alternatively, if the user account was created incorrectly or cannot be
1194 changed, it is recommended to set
1198 in
<filename>/etc/systemd/logind.conf
</filename> or another appropriate
1204 At least one of these two things has to be ensured, or the PostgreSQL
1205 server will be very unreliable.
1210 <sect2 id=
"kernel-resources-limits">
1211 <title>Resource Limits
</title>
1214 Unix-like operating systems enforce various kinds of resource limits
1215 that might interfere with the operation of your
1216 <productname>PostgreSQL
</productname> server. Of particular
1217 importance are limits on the number of processes per user, the
1218 number of open files per process, and the amount of memory available
1219 to each process. Each of these have a
<quote>hard
</quote> and a
1220 <quote>soft
</quote> limit. The soft limit is what actually counts
1221 but it can be changed by the user up to the hard limit. The hard
1222 limit can only be changed by the root user. The system call
1223 <function>setrlimit
</function> is responsible for setting these
1224 parameters. The shell's built-in command
<command>ulimit
</command>
1225 (Bourne shells) or
<command>limit
</command> (
<application>csh
</application>) is
1226 used to control the resource limits from the command line. On
1227 BSD-derived systems the file
<filename>/etc/login.conf
</filename>
1228 controls the various resource limits set during login. See the
1229 operating system documentation for details. The relevant
1230 parameters are
<varname>maxproc
</varname>,
1231 <varname>openfiles
</varname>, and
<varname>datasize
</varname>. For
1236 :datasize-cur=
256M:\
1238 :openfiles-cur=
256:\
1241 (
<literal>-cur
</literal> is the soft limit. Append
1242 <literal>-max
</literal> to set the hard limit.)
1246 Kernels can also have system-wide limits on some resources.
1250 On
<productname>Linux
</productname> the kernel parameter
1251 <varname>fs.file-max
</varname> determines the maximum number of open
1252 files that the kernel will support. It can be changed with
1253 <literal>sysctl -w fs.file-max=
<replaceable>N
</replaceable></literal>.
1254 To make the setting persist across reboots, add an assignment
1255 in
<filename>/etc/sysctl.conf
</filename>.
1256 The maximum limit of files per process is fixed at the time the
1257 kernel is compiled; see
1258 <filename>/usr/src/linux/Documentation/proc.txt
</filename> for
1266 The
<productname>PostgreSQL
</productname> server uses one process
1267 per connection so you should provide for at least as many processes
1268 as allowed connections, in addition to what you need for the rest
1269 of your system. This is usually not a problem but if you run
1270 several servers on one machine things might get tight.
1274 The factory default limit on open files is often set to
1275 <quote>socially friendly
</quote> values that allow many users to
1276 coexist on a machine without using an inappropriate fraction of
1277 the system resources. If you run many servers on a machine this
1278 is perhaps what you want, but on dedicated servers you might want to
1283 On the other side of the coin, some systems allow individual
1284 processes to open large numbers of files; if more than a few
1285 processes do so then the system-wide limit can easily be exceeded.
1286 If you find this happening, and you do not want to alter the
1287 system-wide limit, you can set
<productname>PostgreSQL
</productname>'s
<xref
1288 linkend=
"guc-max-files-per-process"/> configuration parameter to
1289 limit the consumption of open files.
1293 Another kernel limit that may be of concern when supporting large
1294 numbers of client connections is the maximum socket connection queue
1295 length. If more than that many connection requests arrive within a very
1296 short period, some may get rejected before the
<productname>PostgreSQL
</productname> server can service
1297 the requests, with those clients receiving unhelpful connection failure
1298 errors such as
<quote>Resource temporarily unavailable
</quote> or
1299 <quote>Connection refused
</quote>. The default queue length limit is
128
1300 on many platforms. To raise it, adjust the appropriate kernel parameter
1301 via
<application>sysctl
</application>, then restart the
<productname>PostgreSQL
</productname> server.
1302 The parameter is variously named
<varname>net.core.somaxconn
</varname>
1303 on Linux,
<varname>kern.ipc.soacceptqueue
</varname> on newer FreeBSD,
1304 and
<varname>kern.ipc.somaxconn
</varname> on macOS and other BSD
1309 <sect2 id=
"linux-memory-overcommit">
1310 <title>Linux Memory Overcommit
</title>
1313 <primary>memory overcommit
</primary>
1317 <primary>OOM
</primary>
1321 <primary>overcommit
</primary>
1325 The default virtual memory behavior on Linux is not
1326 optimal for
<productname>PostgreSQL
</productname>. Because of the
1327 way that the kernel implements memory overcommit, the kernel might
1328 terminate the
<productname>PostgreSQL
</productname> postmaster (the
1329 supervisor server process) if the memory demands of either
1330 <productname>PostgreSQL
</productname> or another process cause the
1331 system to run out of virtual memory.
1335 If this happens, you will see a kernel message that looks like
1336 this (consult your system documentation and configuration on where
1337 to look for such a message):
1339 Out of Memory: Killed process
12345 (postgres).
1341 This indicates that the
<filename>postgres
</filename> process
1342 has been terminated due to memory pressure.
1343 Although existing database connections will continue to function
1344 normally, no new connections will be accepted. To recover,
1345 <productname>PostgreSQL
</productname> will need to be restarted.
1349 One way to avoid this problem is to run
1350 <productname>PostgreSQL
</productname> on a machine where you can
1351 be sure that other processes will not run the machine out of
1352 memory. If memory is tight, increasing the swap space of the
1353 operating system can help avoid the problem, because the
1354 out-of-memory (OOM) killer is invoked only when physical memory and
1355 swap space are exhausted.
1359 If
<productname>PostgreSQL
</productname> itself is the cause of the
1360 system running out of memory, you can avoid the problem by changing
1361 your configuration. In some cases, it may help to lower memory-related
1362 configuration parameters, particularly
1363 <link linkend=
"guc-shared-buffers"><varname>shared_buffers
</varname></link>,
1364 <link linkend=
"guc-work-mem"><varname>work_mem
</varname></link>, and
1365 <link linkend=
"guc-hash-mem-multiplier"><varname>hash_mem_multiplier
</varname></link>.
1366 In other cases, the problem may be caused by allowing too many
1367 connections to the database server itself. In many cases, it may
1369 <link linkend=
"guc-max-connections"><varname>max_connections
</varname></link>
1370 and instead make use of external connection-pooling software.
1374 It is possible to modify the
1375 kernel's behavior so that it will not
<quote>overcommit
</quote> memory.
1376 Although this setting will not prevent the
<ulink
1377 url=
"https://lwn.net/Articles/104179/">OOM killer
</ulink> from being invoked
1378 altogether, it will lower the chances significantly and will therefore
1379 lead to more robust system behavior. This is done by selecting strict
1380 overcommit mode via
<command>sysctl
</command>:
1382 sysctl -w vm.overcommit_memory=
2
1384 or placing an equivalent entry in
<filename>/etc/sysctl.conf
</filename>.
1385 You might also wish to modify the related setting
1386 <varname>vm.overcommit_ratio
</varname>. For details see the kernel documentation
1387 file
<ulink url=
"https://www.kernel.org/doc/Documentation/vm/overcommit-accounting"></ulink>.
1391 Another approach, which can be used with or without altering
1392 <varname>vm.overcommit_memory
</varname>, is to set the process-specific
1393 <firstterm>OOM score adjustment
</firstterm> value for the postmaster process to
1394 <literal>-
1000</literal>, thereby guaranteeing it will not be targeted by the OOM
1395 killer. The simplest way to do this is to execute
1397 echo -
1000 > /proc/self/oom_score_adj
1399 in the
<productname>PostgreSQL
</productname> startup script just before
1400 invoking
<filename>postgres
</filename>.
1401 Note that this action must be done as root, or it will have no effect;
1402 so a root-owned startup script is the easiest place to do it. If you
1403 do this, you should also set these environment variables in the startup
1404 script before invoking
<filename>postgres
</filename>:
1406 export PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
1407 export PG_OOM_ADJUST_VALUE=
0
1409 These settings will cause postmaster child processes to run with the
1410 normal OOM score adjustment of zero, so that the OOM killer can still
1411 target them at need. You could use some other value for
1412 <envar>PG_OOM_ADJUST_VALUE
</envar> if you want the child processes to run
1413 with some other OOM score adjustment. (
<envar>PG_OOM_ADJUST_VALUE
</envar>
1414 can also be omitted, in which case it defaults to zero.) If you do not
1415 set
<envar>PG_OOM_ADJUST_FILE
</envar>, the child processes will run with the
1416 same OOM score adjustment as the postmaster, which is unwise since the
1417 whole point is to ensure that the postmaster has a preferential setting.
1422 <sect2 id=
"linux-huge-pages">
1423 <title>Linux Huge Pages
</title>
1426 Using huge pages reduces overhead when using large contiguous chunks of
1427 memory, as
<productname>PostgreSQL
</productname> does, particularly when
1428 using large values of
<xref linkend=
"guc-shared-buffers"/>. To use this
1429 feature in
<productname>PostgreSQL
</productname> you need a kernel
1430 with
<varname>CONFIG_HUGETLBFS=y
</varname> and
1431 <varname>CONFIG_HUGETLB_PAGE=y
</varname>. You will also have to configure
1432 the operating system to provide enough huge pages of the desired size.
1433 The runtime-computed parameter
1434 <xref linkend=
"guc-shared-memory-size-in-huge-pages"/> reports the number
1435 of huge pages required. This parameter can be viewed before starting the
1436 server with a
<command>postgres
</command> command like:
1438 $
<userinput>postgres -D $PGDATA -C shared_memory_size_in_huge_pages
</userinput>
1440 $
<userinput>grep ^Hugepagesize /proc/meminfo
</userinput>
1441 Hugepagesize:
2048 kB
1442 $
<userinput>ls /sys/kernel/mm/hugepages
</userinput>
1443 hugepages-
1048576kB hugepages-
2048kB
1446 In this example the default is
2MB, but you can also explicitly request
1447 either
2MB or
1GB with
<xref linkend=
"guc-huge-page-size"/> to adapt
1448 the number of pages calculated by
1449 <varname>shared_memory_size_in_huge_pages
</varname>.
1451 While we need at least
<literal>3170</literal> huge pages in this example,
1452 a larger setting would be appropriate if other programs on the machine
1453 also need huge pages.
1454 We can set this with:
1456 #
<userinput>sysctl -w vm.nr_hugepages=
3170</userinput>
1458 Don't forget to add this setting to
<filename>/etc/sysctl.conf
</filename>
1459 so that it is reapplied after reboots. For non-default huge page sizes,
1462 #
<userinput>echo
3170 > /sys/kernel/mm/hugepages/hugepages-
2048kB/nr_hugepages
</userinput>
1464 It is also possible to provide these settings at boot time using
1465 kernel parameters such as
<literal>hugepagesz=
2M hugepages=
3170</literal>.
1469 Sometimes the kernel is not able to allocate the desired number of huge
1470 pages immediately due to fragmentation, so it might be necessary
1471 to repeat the command or to reboot. (Immediately after a reboot, most of
1472 the machine's memory should be available to convert into huge pages.)
1473 To verify the huge page allocation situation for a given size, use:
1475 $
<userinput>cat /sys/kernel/mm/hugepages/hugepages-
2048kB/nr_hugepages
</userinput>
1480 It may also be necessary to give the database server's operating system
1481 user permission to use huge pages by setting
1482 <varname>vm.hugetlb_shm_group
</varname> via
<application>sysctl
</application>, and/or
1483 give permission to lock memory with
<command>ulimit -l
</command>.
1487 The default behavior for huge pages in
1488 <productname>PostgreSQL
</productname> is to use them when possible, with
1489 the system's default huge page size, and
1490 to fall back to normal pages on failure. To enforce the use of huge
1491 pages, you can set
<xref linkend=
"guc-huge-pages"/>
1492 to
<literal>on
</literal> in
<filename>postgresql.conf
</filename>.
1493 Note that with this setting
<productname>PostgreSQL
</productname> will fail to
1494 start if not enough huge pages are available.
1498 For a detailed description of the
<productname>Linux
</productname> huge
1499 pages feature have a look
1500 at
<ulink url=
"https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt"></ulink>.
1507 <sect1 id=
"server-shutdown">
1508 <title>Shutting Down the Server
</title>
1510 <indexterm zone=
"server-shutdown">
1511 <primary>shutdown
</primary>
1515 There are several ways to shut down the database server.
1516 Under the hood, they all reduce to sending a signal to the supervisor
1517 <command>postgres
</command> process.
1521 If you are using a pre-packaged version
1522 of
<productname>PostgreSQL
</productname>, and you used its provisions
1523 for starting the server, then you should also use its provisions for
1524 stopping the server. Consult the package-level documentation for
1529 When managing the server directly, you can control the type of shutdown
1530 by sending different signals to the
<command>postgres
</command>
1535 <term><systemitem>SIGTERM
</systemitem><indexterm><primary>SIGTERM
</primary></indexterm></term>
1538 This is the
<firstterm>Smart Shutdown
</firstterm> mode.
1539 After receiving
<systemitem>SIGTERM
</systemitem>, the server
1540 disallows new connections, but lets existing sessions end their
1541 work normally. It shuts down only after all of the sessions terminate.
1542 If the server is in recovery when a smart
1543 shutdown is requested, recovery and streaming replication will be
1544 stopped only after all regular sessions have terminated.
1550 <term><systemitem>SIGINT
</systemitem><indexterm><primary>SIGINT
</primary></indexterm></term>
1553 This is the
<firstterm>Fast Shutdown
</firstterm> mode.
1554 The server disallows new connections and sends all existing
1555 server processes
<systemitem>SIGTERM
</systemitem>, which will cause them
1556 to abort their current transactions and exit promptly. It then
1557 waits for all server processes to exit and finally shuts down.
1563 <term><systemitem>SIGQUIT
</systemitem><indexterm><primary>SIGQUIT
</primary></indexterm></term>
1566 This is the
<firstterm>Immediate Shutdown
</firstterm> mode.
1567 The server will send
<systemitem>SIGQUIT
</systemitem> to all child
1568 processes and wait for them to terminate. If any do not terminate
1569 within
5 seconds, they will be sent
<systemitem>SIGKILL
</systemitem>.
1570 The supervisor server process exits as soon as all child processes have
1571 exited, without doing normal database shutdown processing.
1572 This will lead to recovery (by
1573 replaying the WAL log) upon next start-up. This is recommended
1574 only in emergencies.
1582 The
<xref linkend=
"app-pg-ctl"/> program provides a convenient
1583 interface for sending these signals to shut down the server.
1584 Alternatively, you can send the signal directly using
<command>kill
</command>
1585 on non-Windows systems.
1586 The
<acronym>PID
</acronym> of the
<command>postgres
</command> process can be
1587 found using the
<command>ps
</command> program, or from the file
1588 <filename>postmaster.pid
</filename> in the data directory. For
1589 example, to do a fast shutdown:
1591 $
<userinput>kill -INT `head -
1 /usr/local/pgsql/data/postmaster.pid`
</userinput>
1597 It is best not to use
<systemitem>SIGKILL
</systemitem> to shut down the
1598 server. Doing so will prevent the server from releasing shared memory and
1599 semaphores. Furthermore,
<systemitem>SIGKILL
</systemitem> kills
1600 the
<command>postgres
</command> process without letting it relay the
1601 signal to its subprocesses, so it might be necessary to kill the
1602 individual subprocesses by hand as well.
1607 To terminate an individual session while allowing other sessions to
1608 continue, use
<function>pg_terminate_backend()
</function> (see
<xref
1609 linkend=
"functions-admin-signal-table"/>) or send a
1610 <systemitem>SIGTERM
</systemitem> signal to the child process associated with
1615 <sect1 id=
"upgrading">
1616 <title>Upgrading a
<productname>PostgreSQL
</productname> Cluster
</title>
1618 <indexterm zone=
"upgrading">
1619 <primary>upgrading
</primary>
1622 <indexterm zone=
"upgrading">
1623 <primary>version
</primary>
1624 <secondary>compatibility
</secondary>
1628 This section discusses how to upgrade your database data from one
1629 <productname>PostgreSQL
</productname> release to a newer one.
1633 Current
<productname>PostgreSQL
</productname> version numbers consist of a
1634 major and a minor version number. For example, in the version number
10.1,
1635 the
10 is the major version number and the
1 is the minor version number,
1636 meaning this would be the first minor release of the major release
10. For
1637 releases before
<productname>PostgreSQL
</productname> version
10.0, version
1638 numbers consist of three numbers, for example,
9.5.3. In those cases, the
1639 major version consists of the first two digit groups of the version number,
1640 e.g.,
9.5, and the minor version is the third number, e.g.,
3, meaning this
1641 would be the third minor release of the major release
9.5.
1645 Minor releases never change the internal storage format and are always
1646 compatible with earlier and later minor releases of the same major version
1647 number. For example, version
10.1 is compatible with version
10.0 and
1648 version
10.6. Similarly, for example,
9.5.3 is compatible with
9.5.0,
1649 9.5.1, and
9.5.6. To update between compatible versions, you simply
1650 replace the executables while the server is down and restart the server.
1651 The data directory remains unchanged
— minor upgrades are that
1656 For
<emphasis>major
</emphasis> releases of
<productname>PostgreSQL
</productname>, the
1657 internal data storage format is subject to change, thus complicating
1658 upgrades. The traditional method for moving data to a new major version
1659 is to dump and restore the database, though this can be slow. A
1660 faster method is
<xref linkend=
"pgupgrade"/>. Replication methods are
1661 also available, as discussed below.
1662 (If you are using a pre-packaged version
1663 of
<productname>PostgreSQL
</productname>, it may provide scripts to
1664 assist with major version upgrades. Consult the package-level
1665 documentation for details.)
1669 New major versions also typically introduce some user-visible
1670 incompatibilities, so application programming changes might be required.
1671 All user-visible changes are listed in the release notes (
<xref
1672 linkend=
"release"/>); pay particular attention to the section
1673 labeled
"Migration". Though you can upgrade from one major version
1674 to another without upgrading to intervening versions, you should read
1675 the major release notes of all intervening versions.
1679 Cautious users will want to test their client applications on the new
1680 version before switching over fully; therefore, it's often a good idea to
1681 set up concurrent installations of old and new versions. When
1682 testing a
<productname>PostgreSQL
</productname> major upgrade, consider the
1683 following categories of possible changes:
1689 <term>Administration
</term>
1692 The capabilities available for administrators to monitor and control
1693 the server often change and improve in each major release.
1702 Typically this includes new SQL command capabilities and not changes
1703 in behavior, unless specifically mentioned in the release notes.
1709 <term>Library API
</term>
1712 Typically libraries like
<application>libpq
</application> only add new
1713 functionality, again unless mentioned in the release notes.
1719 <term>System Catalogs
</term>
1722 System catalog changes usually only affect database management tools.
1728 <term>Server C-language API
</term>
1731 This involves changes in the backend function API, which is written
1732 in the C programming language. Such changes affect code that
1733 references backend functions deep inside the server.
1740 <sect2 id=
"upgrading-via-pgdumpall">
1741 <title>Upgrading Data via
<application>pg_dumpall
</application></title>
1744 One upgrade method is to dump data from one major version of
1745 <productname>PostgreSQL
</productname> and restore it in another
— to do
1746 this, you must use a
<emphasis>logical
</emphasis> backup tool like
1747 <application>pg_dumpall
</application>; file system
1748 level backup methods will not work. (There are checks in place that prevent
1749 you from using a data directory with an incompatible version of
1750 <productname>PostgreSQL
</productname>, so no great harm can be done by
1751 trying to start the wrong server version on a data directory.)
1755 It is recommended that you use the
<application>pg_dump
</application> and
1756 <application>pg_dumpall
</application> programs from the
<emphasis>newer
</emphasis>
1758 <productname>PostgreSQL
</productname>, to take advantage of enhancements
1759 that might have been made in these programs. Current releases of the
1760 dump programs can read data from any server version back to
9.2.
1764 These instructions assume that your existing installation is under the
1765 <filename>/usr/local/pgsql
</filename> directory, and that the data area is in
1766 <filename>/usr/local/pgsql/data
</filename>. Substitute your paths
1773 If making a backup, make sure that your database is not being updated.
1774 This does not affect the integrity of the backup, but the changed
1775 data would of course not be included. If necessary, edit the
1776 permissions in the file
<filename>/usr/local/pgsql/data/pg_hba.conf
</filename>
1777 (or equivalent) to disallow access from everyone except you.
1778 See
<xref linkend=
"client-authentication"/> for additional information on
1784 <primary>pg_dumpall
</primary>
1785 <secondary>use during upgrade
</secondary>
1788 To back up your database installation, type:
1790 <userinput>pg_dumpall
> <replaceable>outputfile
</replaceable></userinput>
1795 To make the backup, you can use the
<application>pg_dumpall
</application>
1796 command from the version you are currently running; see
<xref
1797 linkend=
"backup-dump-all"/> for more details. For best
1798 results, however, try to use the
<application>pg_dumpall
</application>
1799 command from
<productname>PostgreSQL
</productname> &version;,
1800 since this version contains bug fixes and improvements over older
1801 versions. While this advice might seem idiosyncratic since you
1802 haven't installed the new version yet, it is advisable to follow
1803 it if you plan to install the new version in parallel with the
1804 old version. In that case you can complete the installation
1805 normally and transfer the data later. This will also decrease
1812 Shut down the old server:
1814 <userinput>pg_ctl stop
</userinput>
1816 On systems that have
<productname>PostgreSQL
</productname> started at boot time,
1817 there is probably a start-up file that will accomplish the same thing. For
1818 example, on a
<systemitem class=
"osname">Red Hat Linux
</systemitem> system one
1819 might find that this works:
1821 <userinput>/etc/rc.d/init.d/postgresql stop
</userinput>
1823 See
<xref linkend=
"runtime"/> for details about starting and
1824 stopping the server.
1830 If restoring from backup, rename or delete the old installation
1831 directory if it is not version-specific. It is a good idea to
1832 rename the directory, rather than
1833 delete it, in case you have trouble and need to revert to it. Keep
1834 in mind the directory might consume significant disk space. To rename
1835 the directory, use a command like this:
1837 <userinput>mv /usr/local/pgsql /usr/local/pgsql.old
</userinput>
1839 (Be sure to move the directory as a single unit so relative paths
1846 Install the new version of
<productname>PostgreSQL
</productname> as
1847 outlined in
<xref linkend=
"installation"/>.
1853 Create a new database cluster if needed. Remember that you must
1854 execute these commands while logged in to the special database user
1855 account (which you already have if you are upgrading).
1857 <userinput>/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
</userinput>
1864 Restore your previous
<filename>pg_hba.conf
</filename> and any
1865 <filename>postgresql.conf
</filename> modifications.
1871 Start the database server, again using the special database user
1874 <userinput>/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
</userinput>
1881 Finally, restore your data from backup with:
1883 <userinput>/usr/local/pgsql/bin/psql -d postgres -f
<replaceable>outputfile
</replaceable></userinput>
1885 using the
<emphasis>new
</emphasis> <application>psql
</application>.
1891 The least downtime can be achieved by installing the new server in
1892 a different directory and running both the old and the new servers
1893 in parallel, on different ports. Then you can use something like:
1896 pg_dumpall -p
5432 | psql -d postgres -p
5433
1898 to transfer your data.
1903 <sect2 id=
"upgrading-via-pg-upgrade">
1904 <title>Upgrading Data via
<application>pg_upgrade
</application></title>
1907 The
<xref linkend=
"pgupgrade"/> module allows an installation to
1908 be migrated in-place from one major
<productname>PostgreSQL
</productname>
1909 version to another. Upgrades can be performed in minutes,
1910 particularly with
<option>--link
</option> mode. It requires steps similar to
1911 <application>pg_dumpall
</application> above, e.g., starting/stopping the server,
1912 running
<application>initdb
</application>. The
<application>pg_upgrade
</application> <link
1913 linkend=
"pgupgrade">documentation
</link> outlines the necessary steps.
1918 <sect2 id=
"upgrading-via-replication">
1919 <title>Upgrading Data via Replication
</title>
1922 It is also possible to use logical replication methods to create a standby
1923 server with the updated version of
<productname>PostgreSQL
</productname>.
1924 This is possible because logical replication supports
1925 replication between different major versions of
1926 <productname>PostgreSQL
</productname>. The standby can be on the same computer or
1927 a different computer. Once it has synced up with the primary server
1928 (running the older version of
<productname>PostgreSQL
</productname>), you can
1929 switch primaries and make the standby the primary and shut down the older
1930 database instance. Such a switch-over results in only several seconds
1931 of downtime for an upgrade.
1935 This method of upgrading can be performed using the built-in logical
1936 replication facilities as well as using external logical replication
1937 systems such as
<productname>pglogical
</productname>,
1938 <productname>Slony
</productname>,
<productname>Londiste
</productname>, and
1939 <productname>Bucardo
</productname>.
1944 <sect1 id=
"preventing-server-spoofing">
1945 <title>Preventing Server Spoofing
</title>
1947 <indexterm zone=
"preventing-server-spoofing">
1948 <primary>server spoofing
</primary>
1952 While the server is running, it is not possible for a malicious user
1953 to take the place of the normal database server. However, when the
1954 server is down, it is possible for a local user to spoof the normal
1955 server by starting their own server. The spoof server could read
1956 passwords and queries sent by clients, but could not return any data
1957 because the
<varname>PGDATA
</varname> directory would still be secure because
1958 of directory permissions. Spoofing is possible because any user can
1959 start a database server; a client cannot identify an invalid server
1960 unless it is specially configured.
1964 One way to prevent spoofing of
<literal>local
</literal>
1965 connections is to use a Unix domain socket directory (
<xref
1966 linkend=
"guc-unix-socket-directories"/>) that has write permission only
1967 for a trusted local user. This prevents a malicious user from creating
1968 their own socket file in that directory. If you are concerned that
1969 some applications might still reference
<filename>/tmp
</filename> for the
1970 socket file and hence be vulnerable to spoofing, during operating system
1971 startup create a symbolic link
<filename>/tmp/.s.PGSQL
.5432</filename> that points
1972 to the relocated socket file. You also might need to modify your
1973 <filename>/tmp
</filename> cleanup script to prevent removal of the symbolic link.
1977 Another option for
<literal>local
</literal> connections is for clients to use
1978 <link linkend=
"libpq-connect-requirepeer"><literal>requirepeer
</literal></link>
1979 to specify the required owner of the server process connected to
1984 To prevent spoofing on TCP connections, either use
1985 SSL certificates and make sure that clients check the server's certificate,
1986 or use GSSAPI encryption (or both, if they're on separate connections).
1990 To prevent spoofing with SSL, the server
1991 must be configured to accept only
<literal>hostssl
</literal> connections (
<xref
1992 linkend=
"auth-pg-hba-conf"/>) and have SSL key and certificate files
1993 (
<xref linkend=
"ssl-tcp"/>). The TCP client must connect using
1994 <literal>sslmode=verify-ca
</literal> or
1995 <literal>verify-full
</literal> and have the appropriate root certificate
1996 file installed (
<xref linkend=
"libq-ssl-certificates"/>). Alternatively the
1997 system CA pool can be used using
<literal>sslrootcert=system
</literal>; in
1998 this case,
<literal>sslmode=verify-full
</literal> is forced for safety, since
1999 it is generally trivial to obtain certificates which are signed by a public
2004 To prevent server spoofing from occurring when using
2005 <link linkend=
"auth-password">scram-sha-
256</link> password authentication
2006 over a network, you should ensure that you connect to the server using SSL
2007 and with one of the anti-spoofing methods described in the previous
2008 paragraph. Additionally, the SCRAM implementation in
2009 <application>libpq
</application> cannot protect the entire authentication
2010 exchange, but using the
<literal>channel_binding=require
</literal> connection
2011 parameter provides a mitigation against server spoofing. An attacker that
2012 uses a rogue server to intercept a SCRAM exchange can use offline analysis to
2013 potentially determine the hashed password from the client.
2017 To prevent spoofing with GSSAPI, the server must be configured to accept
2018 only
<literal>hostgssenc
</literal> connections
2019 (
<xref linkend=
"auth-pg-hba-conf"/>) and use
<literal>gss
</literal>
2020 authentication with them. The TCP client must connect
2021 using
<literal>gssencmode=require
</literal>.
2025 <sect1 id=
"encryption-options">
2026 <title>Encryption Options
</title>
2028 <indexterm zone=
"encryption-options">
2029 <primary>encryption
</primary>
2033 <productname>PostgreSQL
</productname> offers encryption at several
2034 levels, and provides flexibility in protecting data from disclosure
2035 due to database server theft, unscrupulous administrators, and
2036 insecure networks. Encryption might also be required to secure
2037 sensitive data such as medical records or financial transactions.
2043 <term>Password Encryption
</term>
2047 Database user passwords are stored as hashes (determined by the setting
2048 <xref linkend=
"guc-password-encryption"/>), so the administrator cannot
2049 determine the actual password assigned to the user. If SCRAM or MD5
2050 encryption is used for client authentication, the unencrypted password is
2051 never even temporarily present on the server because the client encrypts
2052 it before being sent across the network. SCRAM is preferred, because it
2053 is an Internet standard and is more secure than the PostgreSQL-specific
2054 MD5 authentication protocol.
2059 Support for MD5-encrypted passwords is deprecated and will be removed in
2060 a future release of
<productname>PostgreSQL
</productname>. Refer to
2061 <xref linkend=
"auth-password"/> for details about migrating to another
2070 <term>Encryption For Specific Columns
</term>
2074 The
<xref linkend=
"pgcrypto"/> module allows certain fields to be
2076 This is useful if only some of the data is sensitive.
2077 The client supplies the decryption key and the data is decrypted
2078 on the server and then sent to the client.
2082 The decrypted data and the decryption key are present on the
2083 server for a brief time while it is being decrypted and
2084 communicated between the client and server. This presents a brief
2085 moment where the data and keys can be intercepted by someone with
2086 complete access to the database server, such as the system
2093 <term>Data Partition Encryption
</term>
2097 Storage encryption can be performed at the file system level or the
2098 block level. Linux file system encryption options include eCryptfs
2099 and EncFS, while FreeBSD uses PEFS. Block level or full disk
2100 encryption options include dm-crypt + LUKS on Linux and GEOM
2101 modules geli and gbde on FreeBSD. Many other operating systems
2102 support this functionality, including Windows.
2106 This mechanism prevents unencrypted data from being read from the
2107 drives if the drives or the entire computer is stolen. This does
2108 not protect against attacks while the file system is mounted,
2109 because when mounted, the operating system provides an unencrypted
2110 view of the data. However, to mount the file system, you need some
2111 way for the encryption key to be passed to the operating system,
2112 and sometimes the key is stored somewhere on the host that mounts
2119 <term>Encrypting Data Across A Network
</term>
2123 SSL connections encrypt all data sent across the network: the
2124 password, the queries, and the data returned. The
2125 <filename>pg_hba.conf
</filename> file allows administrators to specify
2126 which hosts can use non-encrypted connections (
<literal>host
</literal>)
2127 and which require SSL-encrypted connections
2128 (
<literal>hostssl
</literal>). Also, clients can specify that they
2129 connect to servers only via SSL.
2133 GSSAPI-encrypted connections encrypt all data sent across the network,
2134 including queries and data returned. (No password is sent across the
2135 network.) The
<filename>pg_hba.conf
</filename> file allows
2136 administrators to specify which hosts can use non-encrypted connections
2137 (
<literal>host
</literal>) and which require GSSAPI-encrypted connections
2138 (
<literal>hostgssenc
</literal>). Also, clients can specify that they
2139 connect to servers only on GSSAPI-encrypted connections
2140 (
<literal>gssencmode=require
</literal>).
2144 <application>Stunnel
</application> or
2145 <application>SSH
</application> can also be used to encrypt
2152 <term>SSL Host Authentication
</term>
2156 It is possible for both the client and server to provide SSL
2157 certificates to each other. It takes some extra configuration
2158 on each side, but this provides stronger verification of identity
2159 than the mere use of passwords. It prevents a computer from
2160 pretending to be the server just long enough to read the password
2161 sent by the client. It also helps prevent
<quote>man in the middle
</quote>
2162 attacks where a computer between the client and server pretends to
2163 be the server and reads and passes all data between the client and
2170 <term>Client-Side Encryption
</term>
2174 If the system administrator for the server's machine cannot be trusted,
2176 for the client to encrypt the data; this way, unencrypted data
2177 never appears on the database server. Data is encrypted on the
2178 client before being sent to the server, and database results have
2179 to be decrypted on the client before being used.
2188 <sect1 id=
"ssl-tcp">
2189 <title>Secure TCP/IP Connections with SSL
</title>
2191 <indexterm zone=
"ssl-tcp">
2192 <primary>SSL
</primary>
2193 <secondary>TLS
</secondary>
2197 <productname>PostgreSQL
</productname> has native support for using
2198 <acronym>SSL
</acronym> connections to encrypt client/server communications
2199 for increased security. This requires that
2200 <productname>OpenSSL
</productname> is installed on both client and
2201 server systems and that support in
<productname>PostgreSQL
</productname> is
2202 enabled at build time (see
<xref linkend=
"installation"/>).
2206 The terms
<acronym>SSL
</acronym> and
<acronym>TLS
</acronym> are often used
2207 interchangeably to mean a secure encrypted connection using a
2208 <acronym>TLS
</acronym> protocol.
<acronym>SSL
</acronym> protocols are the
2209 precursors to
<acronym>TLS
</acronym> protocols, and the term
2210 <acronym>SSL
</acronym> is still used for encrypted connections even though
2211 <acronym>SSL
</acronym> protocols are no longer supported.
2212 <acronym>SSL
</acronym> is used interchangeably with
<acronym>TLS
</acronym>
2213 in
<productname>PostgreSQL
</productname>.
2216 <sect2 id=
"ssl-setup">
2217 <title>Basic Setup
</title>
2220 With
<acronym>SSL
</acronym> support compiled in, the
2221 <productname>PostgreSQL
</productname> server can be started with
2222 support for encrypted connections using
<acronym>TLS
</acronym> protocols
2223 enabled by setting the parameter
2224 <xref linkend=
"guc-ssl"/> to
<literal>on
</literal> in
2225 <filename>postgresql.conf
</filename>. The server will listen for both normal
2226 and
<acronym>SSL
</acronym> connections on the same TCP port, and will negotiate
2227 with any connecting client on whether to use
<acronym>SSL
</acronym>. By
2228 default, this is at the client's option; see
<xref
2229 linkend=
"auth-pg-hba-conf"/> about how to set up the server to require
2230 use of
<acronym>SSL
</acronym> for some or all connections.
2234 To start in
<acronym>SSL
</acronym> mode, files containing the server certificate
2235 and private key must exist. By default, these files are expected to be
2236 named
<filename>server.crt
</filename> and
<filename>server.key
</filename>, respectively, in
2237 the server's data directory, but other names and locations can be specified
2238 using the configuration parameters
<xref linkend=
"guc-ssl-cert-file"/>
2239 and
<xref linkend=
"guc-ssl-key-file"/>.
2243 On Unix systems, the permissions on
<filename>server.key
</filename> must
2244 disallow any access to world or group; achieve this by the command
2245 <command>chmod
0600 server.key
</command>. Alternatively, the file can be
2246 owned by root and have group read access (that is,
<literal>0640</literal>
2247 permissions). That setup is intended for installations where certificate
2248 and key files are managed by the operating system. The user under which
2249 the
<productname>PostgreSQL
</productname> server runs should then be made a
2250 member of the group that has access to those certificate and key files.
2254 If the data directory allows group read access then certificate files may
2255 need to be located outside of the data directory in order to conform to the
2256 security requirements outlined above. Generally, group access is enabled
2257 to allow an unprivileged user to backup the database, and in that case the
2258 backup software will not be able to read the certificate files and will
2263 If the private key is protected with a passphrase, the
2264 server will prompt for the passphrase and will not start until it has
2266 Using a passphrase by default disables the ability to change the server's
2267 SSL configuration without a server restart, but see
<xref
2268 linkend=
"guc-ssl-passphrase-command-supports-reload"/>.
2269 Furthermore, passphrase-protected private keys cannot be used at all
2274 The first certificate in
<filename>server.crt
</filename> must be the
2275 server's certificate because it must match the server's private key.
2276 The certificates of
<quote>intermediate
</quote> certificate authorities
2277 can also be appended to the file. Doing this avoids the necessity of
2278 storing intermediate certificates on clients, assuming the root and
2279 intermediate certificates were created with
<literal>v3_ca
</literal>
2280 extensions. (This sets the certificate's basic constraint of
2281 <literal>CA
</literal> to
<literal>true
</literal>.)
2282 This allows easier expiration of intermediate certificates.
2286 It is not necessary to add the root certificate to
2287 <filename>server.crt
</filename>. Instead, clients must have the root
2288 certificate of the server's certificate chain.
2292 <sect2 id=
"ssl-openssl-config">
2293 <title>OpenSSL Configuration
</title>
2296 <productname>PostgreSQL
</productname> reads the system-wide
2297 <productname>OpenSSL
</productname> configuration file. By default, this
2298 file is named
<filename>openssl.cnf
</filename> and is located in the
2299 directory reported by
<literal>openssl version -d
</literal>.
2300 This default can be overridden by setting environment variable
2301 <envar>OPENSSL_CONF
</envar> to the name of the desired configuration file.
2305 <productname>OpenSSL
</productname> supports a wide range of ciphers
2306 and authentication algorithms, of varying strength. While a list of
2307 ciphers can be specified in the
<productname>OpenSSL
</productname>
2308 configuration file, you can specify ciphers specifically for use by
2309 the database server by modifying
<xref linkend=
"guc-ssl-ciphers"/> in
2310 <filename>postgresql.conf
</filename>.
2315 It is possible to have authentication without encryption overhead by
2316 using
<literal>NULL-SHA
</literal> or
<literal>NULL-MD5
</literal> ciphers. However,
2317 a man-in-the-middle could read and pass communications between client
2318 and server. Also, encryption overhead is minimal compared to the
2319 overhead of authentication. For these reasons NULL ciphers are not
2325 <sect2 id=
"ssl-client-certificates">
2326 <title>Using Client Certificates
</title>
2329 To require the client to supply a trusted certificate,
2330 place certificates of the root certificate authorities
2331 (
<acronym>CA
</acronym>s) you trust in a file in the data
2332 directory, set the parameter
<xref linkend=
"guc-ssl-ca-file"/> in
2333 <filename>postgresql.conf
</filename> to the new file name, and add the
2334 authentication option
<literal>clientcert=verify-ca
</literal> or
2335 <literal>clientcert=verify-full
</literal> to the appropriate
2336 <literal>hostssl
</literal> line(s) in
<filename>pg_hba.conf
</filename>.
2337 A certificate will then be requested from the client during SSL
2338 connection startup. (See
<xref linkend=
"libpq-ssl"/> for a description
2339 of how to set up certificates on the client.)
2343 For a
<literal>hostssl
</literal> entry with
2344 <literal>clientcert=verify-ca
</literal>, the server will verify
2345 that the client's certificate is signed by one of the trusted
2346 certificate authorities. If
<literal>clientcert=verify-full
</literal>
2347 is specified, the server will not only verify the certificate
2348 chain, but it will also check whether the username or its mapping
2349 matches the
<literal>cn
</literal> (Common Name) of the provided certificate.
2350 Note that certificate chain validation is always ensured when the
2351 <literal>cert
</literal> authentication method is used
2352 (see
<xref linkend=
"auth-cert"/>).
2356 Intermediate certificates that chain up to existing root certificates
2357 can also appear in the
<xref linkend=
"guc-ssl-ca-file"/> file if
2358 you wish to avoid storing them on clients (assuming the root and
2359 intermediate certificates were created with
<literal>v3_ca
</literal>
2360 extensions). Certificate Revocation List (CRL) entries are also
2361 checked if the parameter
<xref linkend=
"guc-ssl-crl-file"/> or
2362 <xref linkend=
"guc-ssl-crl-dir"/> is set.
2366 The
<literal>clientcert
</literal> authentication option is available for
2367 all authentication methods, but only in
<filename>pg_hba.conf
</filename> lines
2368 specified as
<literal>hostssl
</literal>. When
<literal>clientcert
</literal> is
2369 not specified, the server verifies the client certificate against its CA
2370 file only if a client certificate is presented and the CA is configured.
2374 There are two approaches to enforce that users provide a certificate during login.
2378 The first approach makes use of the
<literal>cert
</literal> authentication
2379 method for
<literal>hostssl
</literal> entries in
<filename>pg_hba.conf
</filename>,
2380 such that the certificate itself is used for authentication while also
2381 providing ssl connection security. See
<xref linkend=
"auth-cert"/> for details.
2382 (It is not necessary to specify any
<literal>clientcert
</literal> options
2383 explicitly when using the
<literal>cert
</literal> authentication method.)
2384 In this case, the
<literal>cn
</literal> (Common Name) provided in
2385 the certificate is checked against the user name or an applicable mapping.
2389 The second approach combines any authentication method for
<literal>hostssl
</literal>
2390 entries with the verification of client certificates by setting the
2391 <literal>clientcert
</literal> authentication option to
<literal>verify-ca
</literal>
2392 or
<literal>verify-full
</literal>. The former option only enforces that
2393 the certificate is valid, while the latter also ensures that the
2394 <literal>cn
</literal> (Common Name) in the certificate matches
2395 the user name or an applicable mapping.
2399 <sect2 id=
"ssl-server-files">
2400 <title>SSL Server File Usage
</title>
2403 <xref linkend=
"ssl-file-usage"/> summarizes the files that are
2404 relevant to the SSL setup on the server. (The shown file names are default
2405 names. The locally configured names could be different.)
2408 <table id=
"ssl-file-usage">
2409 <title>SSL Server File Usage
</title>
2414 <entry>Contents
</entry>
2415 <entry>Effect
</entry>
2422 <entry><xref linkend=
"guc-ssl-cert-file"/> (
<filename>$PGDATA/server.crt
</filename>)
</entry>
2423 <entry>server certificate
</entry>
2424 <entry>sent to client to indicate server's identity
</entry>
2428 <entry><xref linkend=
"guc-ssl-key-file"/> (
<filename>$PGDATA/server.key
</filename>)
</entry>
2429 <entry>server private key
</entry>
2430 <entry>proves server certificate was sent by the owner; does not indicate
2431 certificate owner is trustworthy
</entry>
2435 <entry><xref linkend=
"guc-ssl-ca-file"/></entry>
2436 <entry>trusted certificate authorities
</entry>
2437 <entry>checks that client certificate is
2438 signed by a trusted certificate authority
</entry>
2442 <entry><xref linkend=
"guc-ssl-crl-file"/></entry>
2443 <entry>certificates revoked by certificate authorities
</entry>
2444 <entry>client certificate must not be on this list
</entry>
2452 The server reads these files at server start and whenever the server
2453 configuration is reloaded. On
<systemitem class=
"osname">Windows
</systemitem>
2454 systems, they are also re-read whenever a new backend process is spawned
2455 for a new client connection.
2459 If an error in these files is detected at server start, the server will
2460 refuse to start. But if an error is detected during a configuration
2461 reload, the files are ignored and the old SSL configuration continues to
2462 be used. On
<systemitem class=
"osname">Windows
</systemitem> systems, if an error in
2463 these files is detected at backend start, that backend will be unable to
2464 establish an SSL connection. In all these cases, the error condition is
2465 reported in the server log.
2469 <sect2 id=
"ssl-certificate-creation">
2470 <title>Creating Certificates
</title>
2473 To create a simple self-signed certificate for the server, valid for
365
2474 days, use the following
<productname>OpenSSL
</productname> command,
2475 replacing
<replaceable>dbhost.yourdomain.com
</replaceable> with the
2478 openssl req -new -x509 -days
365 -nodes -text -out server.crt \
2479 -keyout server.key -subj
"/CN=<replaceable>dbhost.yourdomain.com</replaceable>"
2483 chmod og-rwx server.key
2485 because the server will reject the file if its permissions are more
2487 For more details on how to create your server private key and
2488 certificate, refer to the
<productname>OpenSSL
</productname> documentation.
2492 While a self-signed certificate can be used for testing, a certificate
2493 signed by a certificate authority (
<acronym>CA
</acronym>) (usually an
2494 enterprise-wide root
<acronym>CA
</acronym>) should be used in production.
2498 To create a server certificate whose identity can be validated
2499 by clients, first create a certificate signing request
2500 (
<acronym>CSR
</acronym>) and a public/private key file:
2502 openssl req -new -nodes -text -out root.csr \
2503 -keyout root.key -subj
"/CN=<replaceable>root.yourdomain.com</replaceable>"
2504 chmod og-rwx root.key
2506 Then, sign the request with the key to create a root certificate
2507 authority (using the default
<productname>OpenSSL
</productname>
2508 configuration file location on
<productname>Linux
</productname>):
2510 openssl x509 -req -in root.csr -text -days
3650 \
2511 -extfile /etc/ssl/openssl.cnf -extensions v3_ca \
2512 -signkey root.key -out root.crt
2514 Finally, create a server certificate signed by the new root certificate
2517 openssl req -new -nodes -text -out server.csr \
2518 -keyout server.key -subj
"/CN=<replaceable>dbhost.yourdomain.com</replaceable>"
2519 chmod og-rwx server.key
2521 openssl x509 -req -in server.csr -text -days
365 \
2522 -CA root.crt -CAkey root.key -CAcreateserial \
2525 <filename>server.crt
</filename> and
<filename>server.key
</filename>
2526 should be stored on the server, and
<filename>root.crt
</filename> should
2527 be stored on the client so the client can verify that the server's leaf
2528 certificate was signed by its trusted root certificate.
2529 <filename>root.key
</filename> should be stored offline for use in
2530 creating future certificates.
2534 It is also possible to create a chain of trust that includes
2535 intermediate certificates:
2538 openssl req -new -nodes -text -out root.csr \
2539 -keyout root.key -subj
"/CN=<replaceable>root.yourdomain.com</replaceable>"
2540 chmod og-rwx root.key
2541 openssl x509 -req -in root.csr -text -days
3650 \
2542 -extfile /etc/ssl/openssl.cnf -extensions v3_ca \
2543 -signkey root.key -out root.crt
2546 openssl req -new -nodes -text -out intermediate.csr \
2547 -keyout intermediate.key -subj
"/CN=<replaceable>intermediate.yourdomain.com</replaceable>"
2548 chmod og-rwx intermediate.key
2549 openssl x509 -req -in intermediate.csr -text -days
1825 \
2550 -extfile /etc/ssl/openssl.cnf -extensions v3_ca \
2551 -CA root.crt -CAkey root.key -CAcreateserial \
2552 -out intermediate.crt
2555 openssl req -new -nodes -text -out server.csr \
2556 -keyout server.key -subj
"/CN=<replaceable>dbhost.yourdomain.com</replaceable>"
2557 chmod og-rwx server.key
2558 openssl x509 -req -in server.csr -text -days
365 \
2559 -CA intermediate.crt -CAkey intermediate.key -CAcreateserial \
2562 <filename>server.crt
</filename> and
2563 <filename>intermediate.crt
</filename> should be concatenated
2564 into a certificate file bundle and stored on the server.
2565 <filename>server.key
</filename> should also be stored on the server.
2566 <filename>root.crt
</filename> should be stored on the client so
2567 the client can verify that the server's leaf certificate was signed
2568 by a chain of certificates linked to its trusted root certificate.
2569 <filename>root.key
</filename> and
<filename>intermediate.key
</filename>
2570 should be stored offline for use in creating future certificates.
2576 <sect1 id=
"gssapi-enc">
2577 <title>Secure TCP/IP Connections with GSSAPI Encryption
</title>
2579 <indexterm zone=
"gssapi-enc">
2580 <primary>gssapi
</primary>
2584 <productname>PostgreSQL
</productname> also has native support for
2585 using
<acronym>GSSAPI
</acronym> to encrypt client/server communications for
2586 increased security. Support requires that a
<acronym>GSSAPI
</acronym>
2587 implementation (such as MIT Kerberos) is installed on both client and server
2588 systems, and that support in
<productname>PostgreSQL
</productname> is
2589 enabled at build time (see
<xref linkend=
"installation"/>).
2592 <sect2 id=
"gssapi-setup">
2593 <title>Basic Setup
</title>
2596 The
<productname>PostgreSQL
</productname> server will listen for both
2597 normal and
<acronym>GSSAPI
</acronym>-encrypted connections on the same TCP
2598 port, and will negotiate with any connecting client whether to
2599 use
<acronym>GSSAPI
</acronym> for encryption (and for authentication). By
2600 default, this decision is up to the client (which means it can be
2601 downgraded by an attacker); see
<xref linkend=
"auth-pg-hba-conf"/> about
2602 setting up the server to require the use of
<acronym>GSSAPI
</acronym> for
2603 some or all connections.
2607 When using
<acronym>GSSAPI
</acronym> for encryption, it is common to
2608 use
<acronym>GSSAPI
</acronym> for authentication as well, since the
2609 underlying mechanism will determine both client and server identities
2610 (according to the
<acronym>GSSAPI
</acronym> implementation) in any
2611 case. But this is not required;
2612 another
<productname>PostgreSQL
</productname> authentication method
2613 can be chosen to perform additional verification.
2617 Other than configuration of the negotiation
2618 behavior,
<acronym>GSSAPI
</acronym> encryption requires no setup beyond
2619 that which is necessary for GSSAPI authentication. (For more information
2620 on configuring that, see
<xref linkend=
"gssapi-auth"/>.)
2625 <sect1 id=
"ssh-tunnels">
2626 <title>Secure TCP/IP Connections with
<application>SSH
</application> Tunnels
</title>
2628 <indexterm zone=
"ssh-tunnels">
2629 <primary>ssh
</primary>
2633 It is possible to use
<application>SSH
</application> to encrypt the network
2634 connection between clients and a
2635 <productname>PostgreSQL
</productname> server. Done properly, this
2636 provides an adequately secure network connection, even for non-SSL-capable
2641 First make sure that an
<application>SSH
</application> server is
2642 running properly on the same machine as the
2643 <productname>PostgreSQL
</productname> server and that you can log in using
2644 <command>ssh
</command> as some user; you then can establish a
2645 secure tunnel to the remote server. A secure tunnel listens on a
2646 local port and forwards all traffic to a port on the remote machine.
2647 Traffic sent to the remote port can arrive on its
2648 <literal>localhost
</literal> address, or different bind
2649 address if desired; it does not appear as coming from your
2650 local machine. This command creates a secure tunnel from the client
2651 machine to the remote machine
<literal>foo.com
</literal>:
2653 ssh -L
63333:localhost:
5432 joe@foo.com
2655 The first number in the
<option>-L
</option> argument,
63333, is the
2656 local port number of the tunnel; it can be any unused port. (IANA
2657 reserves ports
49152 through
65535 for private use.) The name or IP
2658 address after this is the remote bind address you are connecting to,
2659 i.e.,
<literal>localhost
</literal>, which is the default. The second
2660 number,
5432, is the remote end of the tunnel, e.g., the port number
2661 your database server is using. In order to connect to the database
2662 server using this tunnel, you connect to port
63333 on the local
2665 psql -h localhost -p
63333 postgres
2667 To the database server it will then look as though you are
2668 user
<literal>joe
</literal> on host
<literal>foo.com
</literal>
2669 connecting to the
<literal>localhost
</literal> bind address, and it
2670 will use whatever authentication procedure was configured for
2671 connections by that user to that bind address. Note that the server will not
2672 think the connection is SSL-encrypted, since in fact it is not
2673 encrypted between the
2674 <application>SSH
</application> server and the
2675 <productname>PostgreSQL
</productname> server. This should not pose any
2676 extra security risk because they are on the same machine.
2681 tunnel setup to succeed you must be allowed to connect via
2682 <command>ssh
</command> as
<literal>joe@foo.com
</literal>, just
2683 as if you had attempted to use
<command>ssh
</command> to create a
2688 You could also have set up port forwarding as
2690 ssh -L
63333:foo.com:
5432 joe@foo.com
2692 but then the database server will see the connection as coming in
2693 on its
<literal>foo.com
</literal> bind address, which is not opened by
2694 the default setting
<literal>listen_addresses =
2695 'localhost'
</literal>. This is usually not what you want.
2699 If you have to
<quote>hop
</quote> to the database server via some
2700 login host, one possible setup could look like this:
2702 ssh -L
63333:db.foo.com:
5432 joe@shell.foo.com
2704 Note that this way the connection
2705 from
<literal>shell.foo.com
</literal>
2706 to
<literal>db.foo.com
</literal> will not be encrypted by the SSH
2708 SSH offers quite a few configuration possibilities when the network
2709 is restricted in various ways. Please refer to the SSH
2710 documentation for details.
2715 Several other applications exist that can provide secure tunnels using
2716 a procedure similar in concept to the one just described.
2722 <sect1 id=
"event-log-registration">
2723 <title>Registering
<application>Event Log
</application> on
<systemitem
2724 class=
"osname">Windows
</systemitem></title>
2726 <indexterm zone=
"event-log-registration">
2727 <primary>event log
</primary>
2728 <secondary>event log
</secondary>
2732 To register a
<systemitem class=
"osname">Windows
</systemitem>
2733 <application>event log
</application> library with the operating system,
2736 <userinput>regsvr32
<replaceable>pgsql_library_directory
</replaceable>/pgevent.dll
</userinput>
2738 This creates registry entries used by the event viewer, under the default
2739 event source named
<literal>PostgreSQL
</literal>.
2743 To specify a different event source name (see
2744 <xref linkend=
"guc-event-source"/>), use the
<literal>/n
</literal>
2745 and
<literal>/i
</literal> options:
2747 <userinput>regsvr32 /n /i:
<replaceable>event_source_name
</replaceable> <replaceable>pgsql_library_directory
</replaceable>/pgevent.dll
</userinput>
2752 To unregister the
<application>event log
</application> library from
2753 the operating system, issue this command:
2755 <userinput>regsvr32 /u [/i:
<replaceable>event_source_name
</replaceable>]
<replaceable>pgsql_library_directory
</replaceable>/pgevent.dll
</userinput>
2761 To enable event logging in the database server, modify
2762 <xref linkend=
"guc-log-destination"/> to include
2763 <literal>eventlog
</literal> in
<filename>postgresql.conf
</filename>.