Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
pg_test_timing — measure timing overhead
pg_test_timing
[option
...]
pg_test_timing is a tool to measure the timing overhead on your system and confirm that the system time never moves backwards. Systems that are slow to collect timing data can give less accurate EXPLAIN ANALYZE
results.
pg_test_timing accepts the following command-line options:-d
duration
--duration=
duration
Specifies the test duration, in seconds. Longer durations give slightly better accuracy, and are more likely to discover problems with the system clock moving backwards. The default test duration is 3 seconds.-V
--version
Print the pg_test_timing version and exit.-?
--help
Show help about pg_test_timing command line arguments, and exit.
Good results will show most (>90%) individual timing calls take less than one microsecond. Average per loop overhead will be even lower, below 100 nanoseconds. This example from an Intel i7-860 system using a TSC clock source shows excellent performance:
Note that different units are used for the per loop time than the histogram. The loop can have resolution within a few nanoseconds (ns), while the individual timing calls can only resolve down to one microsecond (us).
When the query executor is running a statement using EXPLAIN ANALYZE
, individual operations are timed as well as showing a summary. The overhead of your system can be checked by counting rows with the psql program:
The i7-860 system measured runs the count query in 9.8 ms while the EXPLAIN ANALYZE
version takes 16.6 ms, each processing just over 100,000 rows. That 6.8 ms difference means the timing overhead per row is 68 ns, about twice what pg_test_timing estimated it would be. Even that relatively small amount of overhead is making the fully timed count statement take almost 70% longer. On more substantial queries, the timing overhead would be less problematic.
On some newer Linux systems, it's possible to change the clock source used to collect timing data at any time. A second example shows the slowdown possible from switching to the slower acpi_pm time source, on the same system used for the fast results above:
In this configuration, the sample EXPLAIN ANALYZE
above takes 115.9 ms. That's 1061 ns of timing overhead, again a small multiple of what's measured directly by this utility. That much timing overhead means the actual query itself is only taking a tiny fraction of the accounted for time, most of it is being consumed in overhead instead. In this configuration, any EXPLAIN ANALYZE
totals involving many timed operations would be inflated significantly by timing overhead.
FreeBSD also allows changing the time source on the fly, and it logs information about the timer selected during boot:
Other systems may only allow setting the time source on boot. On older Linux systems the "clock" kernel setting is the only way to make this sort of change. And even on some more recent ones, the only option you'll see for a clock source is "jiffies". Jiffies are the older Linux software clock implementation, which can have good resolution when it's backed by fast enough timing hardware, as in this example:
Collecting accurate timing information is normally done on computers using hardware clocks with various levels of accuracy. With some hardware the operating systems can pass the system clock time almost directly to programs. A system clock can also be derived from a chip that simply provides timing interrupts, periodic ticks at some known time interval. In either case, operating system kernels provide a clock source that hides these details. But the accuracy of that clock source and how quickly it can return results varies based on the underlying hardware.
Inaccurate time keeping can result in system instability. Test any change to the clock source very carefully. Operating system defaults are sometimes made to favor reliability over best accuracy. And if you are using a virtual machine, look into the recommended time sources compatible with it. Virtual hardware faces additional difficulties when emulating timers, and there are often per operating system settings suggested by vendors.
The Time Stamp Counter (TSC) clock source is the most accurate one available on current generation CPUs. It's the preferred way to track the system time when it's supported by the operating system and the TSC clock is reliable. There are several ways that TSC can fail to provide an accurate timing source, making it unreliable. Older systems can have a TSC clock that varies based on the CPU temperature, making it unusable for timing. Trying to use TSC on some older multicore CPUs can give a reported time that's inconsistent among multiple cores. This can result in the time going backwards, a problem this program checks for. And even the newest systems can fail to provide accurate TSC timing with very aggressive power saving configurations.
Newer operating systems may check for the known TSC problems and switch to a slower, more stable clock source when they are seen. If your system supports TSC time but doesn't default to that, it may be disabled for a good reason. And some operating systems may not detect all the possible problems correctly, or will allow using TSC even in situations where it's known to be inaccurate.
The High Precision Event Timer (HPET) is the preferred timer on systems where it's available and TSC is not accurate. The timer chip itself is programmable to allow up to 100 nanosecond resolution, but you may not see that much accuracy in your system clock.
Advanced Configuration and Power Interface (ACPI) provides a Power Management (PM) Timer, which Linux refers to as the acpi_pm. The clock derived from acpi_pm will at best provide 300 nanosecond resolution.
Timers used on older PC hardware include the 8254 Programmable Interval Timer (PIT), the real-time clock (RTC), the Advanced Programmable Interrupt Controller (APIC) timer, and the Cyclone timer. These timers aim for millisecond resolution.
pg_ctl — initialize, start, stop, or control a PostgreSQL server
pg_ctl
init[db]
[-D
datadir
] [-s
] [-o
initdb-options
]
pg_ctl
start
[-D
datadir
] [-l
filename
] [-W
] [-t
seconds
] [-s
] [-o
options
] [-p
path
] [-c
]
pg_ctl
stop
[-D
datadir
] [-m
s[mart]
| f[ast]
| i[mmediate]
] [-W
] [-t
seconds
] [-s
]
pg_ctl
restart
[-D
datadir
] [-m
s[mart]
| f[ast]
| i[mmediate]
] [-W
] [-t
seconds
] [-s
] [-o
options
] [-c
]
pg_ctl
reload
[-D
datadir
] [-s
]
pg_ctl
status
[-D
datadir
]
pg_ctl
promote
[-D
datadir
] [-W
] [-t
seconds
] [-s
]
pg_ctl
logrotate
[-D
datadir
] [-s
]
pg_ctl
kill
signal_name
process_id
On Microsoft Windows, also:
pg_ctl
register
[-D
datadir
] [-N
servicename
] [-U
username
] [-P
password
] [-S
a[uto]
| d[emand]
] [-e
source
] [-W
] [-t
seconds
] [-s
] [-o
options
]
pg_ctl
unregister
[-N
servicename
]
pg_ctl is a utility for initializing a PostgreSQL database cluster, starting, stopping, or restarting the PostgreSQL database server (postgres), or displaying the status of a running server. Although the server can be started manually, pg_ctl encapsulates tasks such as redirecting log output and properly detaching from the terminal and process group. It also provides convenient options for controlled shutdown.
The init
or initdb
mode creates a new PostgreSQL database cluster, that is, a collection of databases that will be managed by a single server instance. This mode invokes the initdb
command. See initdb for details.
start
mode launches a new server. The server is started in the background, and its standard input is attached to /dev/null
(or nul
on Windows). On Unix-like systems, by default, the server's standard output and standard error are sent to pg_ctl's standard output (not standard error). The standard output of pg_ctl should then be redirected to a file or piped to another process such as a log rotating program like rotatelogs; otherwise postgres
will write its output to the controlling terminal (from the background) and will not leave the shell's process group. On Windows, by default the server's standard output and standard error are sent to the terminal. These default behaviors can be changed by using -l
to append the server's output to a log file. Use of either -l
or output redirection is recommended.
stop
mode shuts down the server that is running in the specified data directory. Three different shutdown methods can be selected with the -m
option. “Smart” mode waits for all active clients to disconnect and any online backup to finish. If the server is in hot standby, recovery and streaming replication will be terminated once all clients have disconnected. “Fast” mode (the default) does not wait for clients to disconnect and will terminate an online backup in progress. All active transactions are rolled back and clients are forcibly disconnected, then the server is shut down. “Immediate” mode will abort all server processes immediately, without a clean shutdown. This choice will lead to a crash-recovery cycle during the next server start.
restart
mode effectively executes a stop followed by a start. This allows changing the postgres
command-line options, or changing configuration-file options that cannot be changed without restarting the server. If relative paths were used on the command line during server start, restart
might fail unless pg_ctl is executed in the same current directory as it was during server start.
reload
mode simply sends the postgres
server process a SIGHUP signal, causing it to reread its configuration files (postgresql.conf
, pg_hba.conf
, etc.). This allows changing configuration-file options that do not require a full server restart to take effect.
status
mode checks whether a server is running in the specified data directory. If it is, the server's PID and the command line options that were used to invoke it are displayed. If the server is not running, pg_ctl returns an exit status of 3. If an accessible data directory is not specified, pg_ctl returns an exit status of 4.
promote
mode commands the standby server that is running in the specified data directory to end standby mode and begin read-write operations.
logrotate
mode rotates the server log file. For details on how to use this mode with external log rotation tools, see Section 24.3.
kill
mode sends a signal to a specified process. This is primarily valuable on Microsoft Windows which does not have a built-in kill command. Use --help
to see a list of supported signal names.
register
mode registers the PostgreSQL server as a system service on Microsoft Windows. The -S
option allows selection of service start type, either “auto” (start service automatically on system startup) or “demand” (start service on demand).
unregister
mode unregisters a system service on Microsoft Windows. This undoes the effects of the register
command.
-c
--core-files
Attempt to allow server crashes to produce core files, on platforms where this is possible, by lifting any soft resource limit placed on core files. This is useful in debugging or diagnosing problems by allowing a stack trace to be obtained from a failed server process.
-D
datadir
--pgdata=
datadir
Specifies the file system location of the database configuration files. If this option is omitted, the environment variable PGDATA
is used.
-l
filename
--log=
filename
Append the server log output to filename
. If the file does not exist, it is created. The umask is set to 077, so access to the log file is disallowed to other users by default.
-m
mode
--mode=
mode
Specifies the shutdown mode. mode
can be smart
, fast
, or immediate
, or the first letter of one of these three. If this option is omitted, fast
is the default.
-o
options
--options=
options
Specifies options to be passed directly to the postgres
command. -o
can be specified multiple times, with all the given options being passed through.
The options
should usually be surrounded by single or double quotes to ensure that they are passed through as a group.
-o
initdb-options
--options=
initdb-options
Specifies options to be passed directly to the initdb
command. -o
can be specified multiple times, with all the given options being passed through.
The initdb-options
should usually be surrounded by single or double quotes to ensure that they are passed through as a group.
-p
path
Specifies the location of the postgres
executable. By default the postgres
executable is taken from the same directory as pg_ctl
, or failing that, the hard-wired installation directory. It is not necessary to use this option unless you are doing something unusual and get errors that the postgres
executable was not found.
In init
mode, this option analogously specifies the location of the initdb
executable.
-s
--silent
Print only errors, no informational messages.
-t
seconds
--timeout=
seconds
Specifies the maximum number of seconds to wait when waiting for an operation to complete (see option -w
). Defaults to the value of the PGCTLTIMEOUT
environment variable or, if not set, to 60 seconds.
-V
--version
Print the pg_ctl version and exit.
-w
--wait
Wait for the operation to complete. This is supported for the modes start
, stop
, restart
, promote
, and register
, and is the default for those modes.
When waiting, pg_ctl
repeatedly checks the server's PID file, sleeping for a short amount of time between checks. Startup is considered complete when the PID file indicates that the server is ready to accept connections. Shutdown is considered complete when the server removes the PID file. pg_ctl
returns an exit code based on the success of the startup or shutdown.
If the operation does not complete within the timeout (see option -t
), then pg_ctl
exits with a nonzero exit status. But note that the operation might continue in the background and eventually succeed.
-W
--no-wait
Do not wait for the operation to complete. This is the opposite of the option -w
.
If waiting is disabled, the requested action is triggered, but there is no feedback about its success. In that case, the server log file or an external monitoring system would have to be used to check the progress and success of the operation.
In prior releases of PostgreSQL, this was the default except for the stop
mode.
-?
--help
Show help about pg_ctl command line arguments, and exit.
If an option is specified that is valid, but not relevant to the selected operating mode, pg_ctl ignores it.
-e
source
Name of the event source for pg_ctl to use for logging to the event log when running as a Windows service. The default is PostgreSQL
. Note that this only controls messages sent from pg_ctl itself; once started, the server will use the event source specified by its event_source parameter. Should the server fail very early in startup, before that parameter has been set, it might also log using the default event source name PostgreSQL
.
-N
servicename
Name of the system service to register. This name will be used as both the service name and the display name. The default is PostgreSQL
.
-P
password
Password for the user to run the service as.
-S
start-type
Start type of the system service. start-type
can be auto
, or demand
, or the first letter of one of these two. If this option is omitted, auto
is the default.
-U
username
User name for the user to run the service as. For domain users, use the format DOMAIN\username
.
PGCTLTIMEOUT
Default limit on the number of seconds to wait when waiting for startup or shutdown to complete. If not set, the default is 60 seconds.PGDATA
Default data directory location.
Most pg_ctl
modes require knowing the data directory location; therefore, the -D
option is required unless PGDATA
is set.
pg_ctl
, like most other PostgreSQL utilities, also uses the environment variables supported by libpq (see Section 33.14).
For additional variables that affect the server, see postgres.
postmaster.pid
pg_ctl examines this file in the data directory to determine whether the server is currently running.postmaster.opts
If this file exists in the data directory, pg_ctl (in restart
mode) will pass the contents of the file as options to postgres, unless overridden by the -o
option. The contents of this file are also displayed in status
mode.
To start the server, waiting until the server is accepting connections:
To start the server using port 5433, and running without fsync
, use:
To stop the server, use:
The -m
option allows control over how the server shuts down:
Restarting the server is almost equivalent to stopping the server and starting it again, except that by default, pg_ctl
saves and reuses the command line options that were passed to the previously-running instance. To restart the server using the same options as before, use:
But if -o
is specified, that replaces any previous options. To restart using port 5433, disabling fsync
upon restart:
Here is sample status output from pg_ctl:
The second line is the command that would be invoked in restart mode.
pg_standby — supports the creation of a PostgreSQL warm standby server
pg_standby
[option
...] archivelocation
nextwalfile
walfilepath
[restartwalfile
]
pg_standby supports creation of a “warm standby” database server. It is designed to be a production-ready program, as well as a customizable template should you require specific modifications.
pg_standby is designed to be a waiting restore_command
, which is needed to turn a standard archive recovery into a warm standby operation. Other configuration is required as well, all of which is described in the main server manual (see Section 26.2).
To configure a standby server to use pg_standby, put this into its postgresql.conf
configuration file:
where archiveDir
is the directory from which WAL segment files should be restored.
If restartwalfile
is specified, normally by using the %r
macro, then all WAL files logically preceding this file will be removed from archivelocation
. This minimizes the number of files that need to be retained, while preserving crash-restart capability. Use of this parameter is appropriate if the archivelocation
is a transient staging area for this particular standby server, but not when the archivelocation
is intended as a long-term WAL archive area.
pg_standby assumes that archivelocation
is a directory readable by the server-owning user. If restartwalfile
(or -k
) is specified, the archivelocation
directory must be writable too.
There are two ways to fail over to a “warm standby” database server when the master server fails:Smart Failover
In smart failover, the server is brought up after applying all WAL files available in the archive. This results in zero data loss, even if the standby server has fallen behind, but if there is a lot of unapplied WAL it can be a long time before the standby server becomes ready. To trigger a smart failover, create a trigger file containing the word smart
, or just create it and leave it empty.Fast Failover
In fast failover, the server is brought up immediately. Any WAL files in the archive that have not yet been applied will be ignored, and all transactions in those files are lost. To trigger a fast failover, create a trigger file and write the word fast
into it. pg_standby can also be configured to execute a fast failover automatically if no new WAL file appears within a defined interval.
pg_standby accepts the following command-line arguments:
-c
Use cp
or copy
command to restore WAL files from archive. This is the only supported behavior so this option is useless.
-d
Print lots of debug logging output on stderr
.
-k
Remove files from archivelocation
so that no more than this many WAL files before the current one are kept in the archive. Zero (the default) means not to remove any files from archivelocation
. This parameter will be silently ignored if restartwalfile
is specified, since that specification method is more accurate in determining the correct archive cut-off point. Use of this parameter is deprecated as of PostgreSQL 8.3; it is safer and more efficient to specify a restartwalfile
parameter. A too small setting could result in removal of files that are still needed for a restart of the standby server, while a too large setting wastes archive space.
-r
maxretries
Set the maximum number of times to retry the copy command if it fails (default 3). After each failure, we wait for sleeptime
* num_retries
so that the wait time increases progressively. So by default, we will wait 5 secs, 10 secs, then 15 secs before reporting the failure back to the standby server. This will be interpreted as end of recovery and the standby will come up fully as a result.
-s
sleeptime
Set the number of seconds (up to 60, default 5) to sleep between tests to see if the WAL file to be restored is available in the archive yet. The default setting is not necessarily recommended; consult Section 26.2 for discussion.
-t
triggerfile
Specify a trigger file whose presence should cause failover. It is recommended that you use a structured file name to avoid confusion as to which server is being triggered when multiple servers exist on the same system; for example /tmp/pgsql.trigger.5432
.
-V
--version
Print the pg_standby version and exit.
-w
maxwaittime
Set the maximum number of seconds to wait for the next WAL file, after which a fast failover will be performed. A setting of zero (the default) means wait forever. The default setting is not necessarily recommended; consult Section 26.2 for discussion.
-?
--help
Show help about pg_standby command line arguments, and exit.
pg_standby is designed to work with PostgreSQL 8.2 and later.
PostgreSQL 8.3 provides the %r
macro, which is designed to let pg_standby know the last file it needs to keep. With PostgreSQL 8.2, the -k
option must be used if archive cleanup is required. This option remains available in 8.3, but its use is deprecated.
PostgreSQL 8.4 provides the recovery_end_command
option. Without this option a leftover trigger file can be hazardous.
pg_standby is written in C and has an easy-to-modify source code, with specifically designated sections to modify for your own needs
On Linux or Unix systems, you might use:
where the archive directory is physically located on the standby server, so that the archive_command
is accessing it across NFS, but the files are local to the standby (enabling use of ln
). This will:
produce debugging output in standby.log
sleep for 2 seconds between checks for next WAL file availability
stop waiting only when a trigger file called /tmp/pgsql.trigger.5442
appears, and perform failover according to its content
remove the trigger file when recovery ends
remove no-longer-needed files from the archive directory
On Windows, you might use:
Note that backslashes need to be doubled in the archive_command
, but not in the restore_command
or recovery_end_command
. This will:
use the copy
command to restore WAL files from archive
produce debugging output in standby.log
sleep for 5 seconds between checks for next WAL file availability
stop waiting only when a trigger file called C:\pgsql.trigger.5442
appears, and perform failover according to its content
remove the trigger file when recovery ends
remove no-longer-needed files from the archive directory
The copy
command on Windows sets the final file size before the file is completely copied, which would ordinarily confuse pg_standby. Therefore pg_standby waits sleeptime
seconds once it sees the proper file size. GNUWin32's cp
sets the file size only after the file copy is complete.
Since the Windows example uses copy
at both ends, either or both servers might be accessing the archive directory across the network.
Simon Riggs <
simon@2ndquadrant.com
>
pg_archivecleanup — 清理 PostgreSQL WAL 歸檔檔案
pg_archivecleanup
[option
...] archivelocation
oldestkeptwalfile
pg_archivecleanup 設計用於 archive_cleanup_command,以在作為備用伺服器執行時清除 WAL 檔案存檔(請參閱第 26.2 節)。pg_archivecleanup 也可以作為獨立程序來清理 WAL 歸檔檔案。
To configure a standby server to use pg_archivecleanup, put this into its postgresql.conf
configuration file:
where archivelocation
is the directory from which WAL segment files should be removed.
When used within archive_cleanup_command, all WAL files logically preceding the value of the %r
argument will be removed from archivelocation
. This minimizes the number of files that need to be retained, while preserving crash-restart capability. Use of this parameter is appropriate if the archivelocation
is a transient staging area for this particular standby server, but not when the archivelocation
is intended as a long-term WAL archive area, or when multiple standby servers are recovering from the same archive location.
When used as a standalone program all WAL files logically preceding the oldestkeptwalfile
will be removed from archivelocation
. In this mode, if you specify a .partial
or .backup
file name, then only the file prefix will be used as the oldestkeptwalfile
. This treatment of .backup
file name allows you to remove all WAL files archived prior to a specific base backup without error. For example, the following example will remove all files older than WAL file name 000000010000003700000010
:
pg_archivecleanup assumes that archivelocation
is a directory readable and writable by the server-owning user.
pg_archivecleanup accepts the following command-line arguments:
-d
Print lots of debug logging output on stderr
.
-n
Print the names of the files that would have been removed on stdout
(performs a dry run).
-V
--version
Print the pg_archivecleanup version and exit.
-x
extension
Provide an extension that will be stripped from all file names before deciding if they should be deleted. This is typically useful for cleaning up archives that have been compressed during storage, and therefore have had an extension added by the compression program. For example:
-x .gz
.-?
--help
Show help about pg_archivecleanup command line arguments, and exit.
The environment variable PG_COLOR
specifies whether to use color in diagnostic messages. Possible values are always
, auto
and never
.
pg_archivecleanup is designed to work with PostgreSQL 8.0 and later when used as a standalone utility, or with PostgreSQL 9.0 and later when used as an archive cleanup command.
pg_archivecleanup is written in C and has an easy-to-modify source code, with specifically designated sections to modify for your own needs
On Linux or Unix systems, you might use:
where the archive directory is physically located on the standby server, so that the archive_command
is accessing it across NFS, but the files are local to the standby. This will:
produce debugging output in cleanup.log
remove no-longer-needed files from the archive directory
本部分包含 PostgreSQL 伺服器應用程式和支援的實用工具參考訊息。這些指令只能在資料庫伺服器所在的主機上有效運行。PostgreSQL 用戶端工具中列出了其他實用工具。
initdb — create a new PostgreSQL database cluster
pg_archivecleanup — clean up PostgreSQL WAL archive files
pg_checksums — enable, disable or check data checksums in a PostgreSQL database cluste
pg_controldata — display control information of a PostgreSQL database cluster
pg_ctl — initialize, start, stop, or control a PostgreSQL server
pg_resetwal — reset the write-ahead log and other control information of a PostgreSQL database cluster
pg_rewind — synchronize a PostgreSQL data directory with another data directory that was forked from it
pg_test_fsync — determine fastest wal_sync_method
for PostgreSQL
pg_test_timing — measure timing overhead
pg_upgrade — upgrade a PostgreSQL server instance
pg_waldump — display a human-readable rendering of the write-ahead log of a PostgreSQL database cluster
postgres — PostgreSQL database server
postmaster — PostgreSQL database server
initdb — create a new PostgreSQL database cluster
initdb
[option
...] [ --pgdata
| -D
] directory
initdb
creates a new PostgreSQL database cluster. A database cluster is a collection of databases that are managed by a single server instance.
Creating a database cluster consists of creating the directories in which the database data will live, generating the shared catalog tables (tables that belong to the whole cluster rather than to any particular database), and creating the template1
and postgres
databases. When you later create a new database, everything in the template1
database is copied. (Therefore, anything installed in template1
is automatically copied into each database created later.) The postgres
database is a default database meant for use by users, utilities and third party applications.
Although initdb
will attempt to create the specified data directory, it might not have permission if the parent directory of the desired data directory is root-owned. To initialize in such a setup, create an empty data directory as root, then use chown
to assign ownership of that directory to the database user account, then su
to become the database user to run initdb
.
initdb
must be run as the user that will own the server process, because the server needs to have access to the files and directories that initdb
creates. Since the server cannot be run as root, you must not run initdb
as root either. (It will in fact refuse to do so.)
For security reasons the new cluster created by initdb
will only be accessible by the cluster owner by default. The --allow-group-access
option allows any user in the same group as the cluster owner to read files in the cluster. This is useful for performing backups as a non-privileged user.
initdb
initializes the database cluster's default locale and character set encoding. The character set encoding, collation order (LC_COLLATE
) and character set classes (LC_CTYPE
, e.g. upper, lower, digit) can be set separately for a database when it is created. initdb
determines those settings for the template1
database, which will serve as the default for all other databases.
To alter the default collation order or character set classes, use the --lc-collate
and --lc-ctype
options. Collation orders other than C
or POSIX
also have a performance penalty. For these reasons it is important to choose the right locale when running initdb
.
The remaining locale categories can be changed later when the server is started. You can also use --locale
to set the default for all locale categories, including collation order and character set classes. All server locale values (lc_*
) can be displayed via SHOW ALL
. More details can be found in .
To alter the default encoding, use the --encoding
. More details can be found in .
-A
authmethod
--auth=
authmethod
This option specifies the default authentication method for local users used in pg_hba.conf
(host
and local
lines). initdb
will prepopulate pg_hba.conf
entries using the specified authentication method for non-replication as well as replication connections.
Do not use trust
unless you trust all local users on your system. trust
is the default for ease of installation.
--auth-host=
authmethod
This option specifies the authentication method for local users via TCP/IP connections used in pg_hba.conf
(host
lines).
--auth-local=
authmethod
This option specifies the authentication method for local users via Unix-domain socket connections used in pg_hba.conf
(local
lines).
-D
directory
--pgdata=
directory
This option specifies the directory where the database cluster should be stored. This is the only information required by initdb
, but you can avoid writing it by setting the PGDATA
environment variable, which can be convenient since the database server (postgres
) can find the database directory later by the same variable.
-E
encoding
--encoding=
encoding
-g
--allow-group-access
Allows users in the same group as the cluster owner to read all cluster files created by initdb
. This option is ignored on Windows as it does not support POSIX-style group permissions.
-k
--data-checksums
--locale=
locale
--lc-collate=
locale
--lc-ctype=
locale
--lc-messages=
locale
--lc-monetary=
locale
--lc-numeric=
locale
--lc-time=
locale
Like --locale
, but only sets the locale in the specified category.--no-locale
Equivalent to --locale=C
.
-N
--no-sync
By default, initdb
will wait for all files to be written safely to disk. This option causes initdb
to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing, but should not be used when creating a production installation.
--pwfile=
filename
Makes initdb
read the database superuser's password from a file. The first line of the file is taken as the password.
-S
--sync-only
Safely write all database files to disk and exit. This does not perform any of the normal initdb operations.
-T
config
--text-search-config=
config
Selects the user name of the database superuser. This defaults to the name of the effective user running initdb
. It is really not important what the superuser's name is, but one might choose to keep the customary name postgres, even if the operating system user's name is different.
-W
--pwprompt
Makes initdb
prompt for a password to give the database superuser. If you don't plan on using password authentication, this is not important. Otherwise you won't be able to use password authentication until you have a password set up.
-X
directory
--waldir=
directory
This option specifies the directory where the write-ahead log should be stored.
--wal-segsize=
size
Set the WAL segment size, in megabytes. This is the size of each individual file in the WAL log. The default size is 16 megabytes. The value must be a power of 2 between 1 and 1024 (megabytes). This option can only be set during initialization, and cannot be changed later.
It may be useful to adjust this size to control the granularity of WAL log shipping or archiving. Also, in databases with a high volume of WAL, the sheer number of WAL files per directory can become a performance and management problem. Increasing the WAL file size will reduce the number of WAL files.
Other, less commonly used, options are also available:
-d
--debug
Print debugging output from the bootstrap backend and a few other messages of lesser interest for the general public. The bootstrap backend is the program initdb
uses to create the catalog tables. This option generates a tremendous amount of extremely boring output.
-L
directory
Specifies where initdb
should find its input files to initialize the database cluster. This is normally not necessary. You will be told if you need to specify their location explicitly.
-n
--no-clean
By default, when initdb
determines that an error prevented it from completely creating the database cluster, it removes any files it might have created before discovering that it cannot finish the job. This option inhibits tidying-up and is thus useful for debugging.
Other options:
-V
--version
Print the initdb version and exit.
-?
--help
Show help about initdb command line arguments, and exit.
PGDATA
Specifies the directory where the database cluster is to be stored; can be overridden using the -D
option.PG_COLOR
Specifies whether to use color in diagnostics messages. Possible values are always
, auto
, never
.TZ
initdb
can also be invoked via pg_ctl initdb
.
postgres — PostgreSQL database server
postgres
[option
...]
postgres
is the PostgreSQL database server. In order for a client application to access a database it connects (over a network or locally) to a running postgres
instance. The postgres
instance then starts a separate server process to handle the connection.
One postgres
instance always manages the data of exactly one database cluster. A database cluster is a collection of databases that is stored at a common file system location (the “data area”). More than one postgres
instance can run on a system at one time, so long as they use different data areas and different communication ports (see below). When postgres
starts it needs to know the location of the data area. The location must be specified by the -D
option or the PGDATA
environment variable; there is no default. Typically, -D
or PGDATA
points directly to the data area directory created by . Other possible file layouts are discussed in .
By default postgres
starts in the foreground and prints log messages to the standard error stream. In practical applications postgres
should be started as a background process, perhaps at boot time.
The postgres
command can also be called in single-user mode. The primary use for this mode is during bootstrapping by . Sometimes it is used for debugging or disaster recovery; note that running a single-user server is not truly suitable for debugging the server, since no realistic interprocess communication and locking will happen. When invoked in single-user mode from the shell, the user can enter queries and the results will be printed to the screen, but in a form that is more useful for developers than end users. In the single-user mode, the session user will be set to the user with ID 1, and implicit superuser powers are granted to this user. This user does not actually have to exist, so the single-user mode can be used to manually recover from certain kinds of accidental damage to the system catalogs.
postgres
accepts the following command-line arguments. For a detailed discussion of the options consult . You can save typing most of these options by setting up a configuration file. Some (safe) options can also be set from the connecting client in an application-dependent way to apply only for that session. For example, if the environment variable PGOPTIONS
is set, then libpq-based clients will pass that string to the server, which will interpret it as postgres
command-line options.
-B
nbuffers
Sets the number of shared buffers for use by the server processes. The default value of this parameter is chosen automatically by initdb. Specifying this option is equivalent to setting the configuration parameter.
-c
name
=value
-C
name
Prints the value of the named run-time parameter, and exits. (See the -c
option above for details.) This can be used on a running server, and returns values frompostgresql.conf
, modified by any parameters supplied in this invocation. It does not reflect parameters supplied when the cluster was started.
-d
debug-level
Sets the debug level. The higher this value is set, the more debugging output is written to the server log. Values are from 1 to 5. It is also possible to pass -d 0
for a specific session, which will prevent the server log level of the parent postgres
process from being propagated to this session.
-D
datadir
-e
-F
-h
hostname
-i
Allows remote clients to connect via TCP/IP (Internet domain) connections. Without this option, only local connections are accepted. This option is equivalent to setting listen_addresses
to *
in postgresql.conf
or via -h
.
-k
directory
-l
-N
max-connections
-o
extra-options
The command-line-style arguments specified in extra-options
are passed to all server processes started by this postgres
process.
Spaces within extra-options
are considered to separate arguments, unless escaped with a backslash (\
); write \\
to represent a literal backslash. Multiple arguments can also be specified via multiple uses of -o
.
The use of this option is obsolete; all command-line options for server processes can be specified directly on the postgres
command line.
-p
port
Specifies the TCP/IP port or local Unix domain socket file extension on which postgres
is to listen for connections from client applications. Defaults to the value of the PGPORT
environment variable, or if PGPORT
is not set, then defaults to the value established during compilation (normally 5432). If you specify a port other than the default port, then all client applications must specify the same port using either command-line options or PGPORT
.
-s
Print time information and other statistics at the end of each command. This is useful for benchmarking or for use in tuning the number of buffers.
-S
work-mem
-V
--version
Print the postgres version and exit.
--
name
=value
Sets a named run-time parameter; a shorter form of -c
.
--describe-config
This option dumps out the server's internal configuration variables, descriptions, and defaults in tab-delimited COPY
format. It is designed primarily for use by administration tools.
-?
--help
Show help about postgres command line arguments, and exit.
The options described here are used mainly for debugging purposes, and in some cases to assist with recovery of severely damaged databases. There should be no reason to use them in a production database setup. They are listed here only for use by PostgreSQL system developers. Furthermore, these options might change or be removed in a future release without notice.
-f
{ s | i | o | b | t | n | m | h }
Forbids the use of particular scan and join methods: s
and i
disable sequential and index scans respectively, o
, b
and t
disable index-only scans, bitmap index scans, and TID scans respectively, while n
, m
, and h
disable nested-loop, merge and hash joins respectively.
Neither sequential scans nor nested-loop joins can be disabled completely; the -fs
and -fn
options simply discourage the optimizer from using those plan types if it has any other alternative.
-n
This option is for debugging problems that cause a server process to die abnormally. The ordinary strategy in this situation is to notify all other server processes that they must terminate and then reinitialize the shared memory and semaphores. This is because an errant server process could have corrupted some shared state before terminating. This option specifies that postgres
will not reinitialize shared data structures. A knowledgeable system programmer can then use a debugger to examine shared memory and semaphore state.
-O
Allows the structure of system tables to be modified. This is used by initdb
.
-P
Ignore system indexes when reading system tables, but still update the indexes when modifying the tables. This is useful when recovering from damaged system indexes.
-t
pa[rser] | pl[anner] | e[xecutor]
Print timing statistics for each query relating to each of the major system modules. This option cannot be used together with the -s
option.-T
This option is for debugging problems that cause a server process to die abnormally. The ordinary strategy in this situation is to notify all other server processes that they must terminate and then reinitialize the shared memory and semaphores. This is because an errant server process could have corrupted some shared state before terminating. This option specifies that postgres
will stop all other server processes by sending the signal SIGSTOP
, but will not cause them to terminate. This permits system programmers to collect core dumps from all server processes by hand.
-v
protocol
Specifies the version number of the frontend/backend protocol to be used for a particular session. This option is for internal use only.
-W
seconds
A delay of this many seconds occurs when a new server process is started, after it conducts the authentication procedure. This is intended to give an opportunity to attach to the server process with a debugger.
--single
Selects the single-user mode. This must be the first argument on the command line.
database
Specifies the name of the database to be accessed. This must be the last argument on the command line. If it is omitted it defaults to the user name.
-E
Echo all commands to standard output before executing them.
-j
Use semicolon followed by two newlines, rather than just newline, as the command entry terminator.
-r
filename
Send all server log output to filename
. This option is only honored when supplied as a command-line option.
PGCLIENTENCODING
Default character encoding used by clients. (The clients can override this individually.) This value can also be set in the configuration file.
PGDATA
Default data directory location
PGDATESTYLE
PGPORT
Default port number (preferably set in the configuration file)
A failure message suggesting that another server is already running should be checked carefully, for example by using the command
or
depending on your system. If you are certain that no conflicting server is running, you can remove the lock file mentioned in the message and try again.
A failure message indicating inability to bind to a port might indicate that that port is already in use by some non-PostgreSQL process. You might also get this error if you terminate postgres
and immediately restart it using the same port; in this case, you must simply wait a few seconds until the operating system closes the port before trying again. Finally, you might get this error if you specify a port number that your operating system considers to be reserved. For example, many versions of Unix consider port numbers under 1024 to be “trusted” and only permit the Unix superuser to access them.
If at all possible, do not use SIGKILL
to kill the main postgres
server. Doing so will prevent postgres
from freeing the system resources (e.g., shared memory and semaphores) that it holds before terminating. This might cause problems for starting a fresh postgres
run.
To terminate the postgres
server normally, the signals SIGTERM
, SIGINT
, or SIGQUIT
can be used. The first will wait for all clients to terminate before quitting, the second will forcefully disconnect all clients, and the third will quit immediately without proper shutdown, resulting in a recovery run during restart.
The SIGHUP
signal will reload the server configuration files. It is also possible to send SIGHUP
to an individual server process, but that is usually not sensible.
The postgres
server uses SIGQUIT
to tell subordinate server processes to terminate without normal cleanup. This signal should not be used by users. It is also unwise to send SIGKILL
to a server process — the main postgres
process will interpret this as a crash and will force all the sibling processes to quit as part of its standard crash-recovery procedure.
The --
options will not work on FreeBSD or OpenBSD. Use -c
instead. This is a bug in the affected operating systems; a future release of PostgreSQL will provide a workaround if this is not fixed.
To start a single-user mode server, use a command like
Provide the correct path to the database directory with -D
, or make sure that the environment variable PGDATA
is set. Also specify the name of the particular database you want to work in.
Normally, the single-user mode server treats newline as the command entry terminator; there is no intelligence about semicolons, as there is in psql. To continue a command across multiple lines, you must type backslash just before each newline except the last one. The backslash and adjacent newline are both dropped from the input command. Note that this will happen even when within a string literal or comment.
But if you use the -j
command line switch, a single newline does not terminate command entry; instead, the sequence semicolon-newline-newline does. That is, type a semicolon immediately followed by a completely empty line. Backslash-newline is not treated specially in this mode. Again, there is no intelligence about such a sequence appearing within a string literal or comment.
In either input mode, if you type a semicolon that is not just before or part of a command entry terminator, it is considered a command separator. When you do type a command entry terminator, the multiple statements you've entered will be executed as a single transaction.
To quit the session, type EOF (Control+D, usually). If you've entered any text since the last command entry terminator, then EOF will be taken as a command entry terminator, and another EOF will be needed to exit.
Note that the single-user mode server does not provide sophisticated line-editing features (no command history, for example). Single-user mode also does not do any background processing, such as automatic checkpoints or replication.
To start postgres
in the background using default values, type:
To start postgres
with a specific port, e.g. 1234:
To connect to this server using psql, specify this port with the -p option:
or set the environment variable PGPORT
:
Named run-time parameters can be set in either of these styles:
Either form overrides whatever setting might exist for work_mem
in postgresql.conf
. Notice that underscores in parameter names can be written as either underscore or dash on the command line. Except for short-term experiments, it's probably better practice to edit the setting in postgresql.conf
than to rely on a command-line switch to set a parameter.
Selects the encoding of the template database. This will also be the default encoding of any database you create later, unless you override it there. The default is derived from the locale, or SQL_ASCII
if that does not work. The character sets supported by the PostgreSQL server are described in .
Use checksums on data pages to help detect corruption by the I/O system that would otherwise be silent. Enabling checksums may incur a noticeable performance penalty. If set, checksums are calculated for all objects, in all databases. All checksum failures will be reported in the view.
Sets the default locale for the database cluster. If this option is not specified, the locale is inherited from the environment that initdb
runs in. Locale support is described in .
Sets the default text search configuration. See for further information.-U
username
--username=
username
Specifies the default time zone of the created database cluster. The value should be a full time zone name (see ).
This utility, like most other PostgreSQL utilities, also uses the environment variables supported by libpq (see ).
,
Sets a named run-time parameter. The configuration parameters supported by PostgreSQL are described in . Most of the other command line options are in fact short forms of such a parameter assignment. -c
can appear multiple times to set multiple parameters.
This option is meant for other programs that interact with a server instance, such as , to query configuration parameter values. User-facing applications should instead use or the pg_settings
view.
Specifies the file system location of the database configuration files. See for details.
Sets the default date style to “European”, that is DMY
ordering of input date fields. This also causes the day to be printed before the month in certain date output formats. See for more information.
Disables fsync
calls for improved performance, at the risk of data corruption in the event of a system crash. Specifying this option is equivalent to disabling the configuration parameter. Read the detailed documentation before using this!
Specifies the IP host name or address on which postgres
is to listen for TCP/IP connections from client applications. The value can also be a comma-separated list of addresses, or *
to specify listening on all available interfaces. An empty value specifies not listening on any IP addresses, in which case only Unix-domain sockets can be used to connect to the server. Defaults to listening only on localhost. Specifying this option is equivalent to setting the configuration parameter.
This option is deprecated since it does not allow access to the full functionality of . It's usually better to set listen_addresses
directly.
Specifies the directory of the Unix-domain socket on which postgres
is to listen for connections from client applications. The value can also be a comma-separated list of directories. An empty value specifies not listening on any Unix-domain sockets, in which case only TCP/IP sockets can be used to connect to the server. The default value is normally /tmp
, but that can be changed at build time. Specifying this option is equivalent to setting the configuration parameter.
Enables secure connections using SSL. PostgreSQL must have been compiled with support for SSL for this option to be available. For more information on using SSL, refer to .
Sets the maximum number of client connections that this server will accept. The default value of this parameter is chosen automatically by initdb. Specifying this option is equivalent to setting the configuration parameter.
Specifies the amount of memory to be used by internal sorts and hashes before resorting to temporary disk files. See the description of the work_mem
configuration parameter in .
The following options only apply to the single-user mode (see ).
Default value of the run-time parameter. (The use of this environment variable is deprecated.)
A failure message mentioning semget
or shmget
probably indicates you need to configure your kernel to provide adequate shared memory and semaphores. For more discussion see . You might be able to postpone reconfiguring your kernel by decreasing to reduce the shared memory consumption of PostgreSQL, and/or by reducing to reduce the semaphore consumption.
The utility command can be used to start and shut down the postgres
server safely and comfortably.
To cancel a running query, send the SIGINT
signal to the process running that command. To terminate a backend process cleanly, send SIGTERM
to that process. See also pg_cancel_backend
and pg_terminate_backend
in for the SQL-callable equivalents of these two actions.
,