Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
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
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 是一個工具程式用於初始化 PostgreSQL 資料庫叢集,啟動、停止或重新啟動 PostgreSQL 資料庫伺服器(postgres)或顯示正在執行中的伺服器狀態。儘管可以手動啟動伺服器,但是 pg_ctl 封裝了諸如重導向日誌輸出以及與終端和程序群組正確分離之類的任務。它還提供了方便的選項以進行可管理的關機程序。
init 或 initdb 模式將建立一個新的 PostgreSQL 資料庫叢集,也就是將由單一個伺服器實例管理的資料庫集合。此模式會呼叫 initdb 指令。有關詳細資訊,請參閱 initdb。
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 disallows new connections, then waits for all existing 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_test_fsync — determine fastest wal_sync_method
for PostgreSQL
pg_test_fsync
[option
...]
pg_test_fsync is intended to give you a reasonable idea of what the fastest wal_sync_method is on your specific system, as well as supplying diagnostic information in the event of an identified I/O problem. However, differences shown by pg_test_fsync might not make any significant difference in real database throughput, especially since many database servers are not speed-limited by their write-ahead logs. pg_test_fsync reports average file sync operation time in microseconds for each wal_sync_method
, which can also be used to inform efforts to optimize the value of commit_delay.
pg_test_fsync accepts the following command-line options:
-f
--filename
Specifies the file name to write test data in. This file should be in the same file system that the pg_wal
directory is or will be placed in. (pg_wal
contains the WAL files.) The default is pg_test_fsync.out
in the current directory.
-s
--secs-per-test
Specifies the number of seconds for each test. The more time per test, the greater the test's accuracy, but the longer it takes to run. The default is 5 seconds, which allows the program to complete in under 2 minutes.
-V
--version
Print the pg_test_fsync version and exit.
-?
--help
Show help about pg_test_fsync 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
.
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 initdb. Other possible file layouts are discussed in Section 19.2.
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 initdb. 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 Chapter 19. 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 shared_buffers configuration parameter.
-c
name
=value
Sets a named run-time parameter. The configuration parameters supported by PostgreSQL are described in Chapter 19. 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.
-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.
This option is meant for other programs that interact with a server instance, such as pg_ctl, to query configuration parameter values. User-facing applications should instead useSHOW or the pg_settings
view.
-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
Specifies the file system location of the database configuration files. See Section 19.2 for details.
-e
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 Section 8.5 for more information.
-F
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 fsyncconfiguration parameter. Read the detailed documentation before using this!
-h
hostname
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 listen_addresses configuration parameter.
-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
.
This option is deprecated since it does not allow access to the full functionality of listen_addresses. It's usually better to set listen_addresses
directly.
-k
directory
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 unix_socket_directories configuration parameter.
-l
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 Section 18.9.
-N
max-connections
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 max_connections configuration parameter.
-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
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 Section 19.4.1.
-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.
The following options only apply to the single-user mode (see Single-User Mode).
--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
Default value of the DateStyle run-time parameter. (The use of this environment variable is deprecated.)
PGPORT
Default port number (preferably set in the configuration file)
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 Section 18.4. You might be able to postpone reconfiguring your kernel by decreasing shared_buffers to reduce the shared memory consumption of PostgreSQL, and/or by reducing max_connections to reduce the semaphore consumption.
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.
The utility command pg_ctl can be used to start and shut down the postgres
server safely and comfortably.
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.
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 Section 9.26.2 for the SQL-callable equivalents of these two actions.
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.
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_upgrade — 升級 PostgreSQL 伺服器服務
語法
pg_upgrade
-b
oldbindir
-B
newbindir
-d
oldconfigdir
-D
newconfigdir
[option
...]
pg_upgrade(以前稱為 pg_migrator)可以將儲存在 PostgreSQL 資料檔案中的內容升級到更高主版本的 PostgreSQL,而毋需進行資料的匯出匯入,例如從 9.5.8 到 9.6.4 或從 10.7 到 11.2。對於次要版本升級,例如從 9.6.2 升級到 9.6.3 或從 10.1 升級到 10.2,則不需要使用這個工具。
PostgreSQL 的主要發行版會定期增加新的功能,這些新功能通常會變更系統資料表的結構,不過內部資料儲存格式很少更改。pg_upgrade 透過建立新的系統資料表並且簡單地重新連結舊的使用者資料檔案數據來執行快速升級。如果將來的主要發行版曾經以使舊資料格式無法讀取的方式變更了資料儲存格式,則 pg_upgrade 將無法用於此類升級。 (社群會儘量避免這種情況發生。)
pg_upgrade 盡最大努力確保新舊叢集是 binary-compatible.例如,透過檢查相容的編譯設定(包括 32/64 位元的二進位內容)來確保。儘管 pg_upgrade 無法檢查,但是任何外部模組也是二進位相容的,這一點很重要。
pg_upgrade 支援從 8.4.X 及更高版本升級到 PostgreSQL 的目前主要版本,包括快照和 beta 版本。
pg_upgrade 接受以下命令列參數:
-b
bindir
--old-bindir=
bindir
the old PostgreSQL executable directory; environment variable PGBINOLD
-B
bindir
--new-bindir=
bindir
the new PostgreSQL executable directory; default is the directory where pg_upgrade resides; environment variable PGBINNEW
-c
--check
check clusters only, don't change any data
-d
configdir
--old-datadir=
configdir
the old database cluster configuration directory; environment variable PGDATAOLD
-D
configdir
--new-datadir=
configdir
the new database cluster configuration directory; environment variable PGDATANEW
-j
njobs
--jobs=
njobs
number of simultaneous processes or threads to use
-k
--link
use hard links instead of copying files to the new cluster
-o
options
--old-options
options
options to be passed directly to the old postgres
command; multiple option invocations are appended
-O
options
--new-options
options
options to be passed directly to the new postgres
command; multiple option invocations are appended
-p
port
--old-port=
port
the old cluster port number; environment variable PGPORTOLD
-P
port
--new-port=
port
the new cluster port number; environment variable PGPORTNEW
-r
--retain
retain SQL and log files even after successful completion
-s
dir
--socketdir=
dir
directory to use for postmaster sockets during upgrade; default is current working directory; environment variable PGSOCKETDIR
-U
username
--username=
username
cluster's install user name; environment variable PGUSER
-v
--verbose
enable verbose internal logging
-V
--version
display version information, then exit
--clone
Use efficient file cloning (also known as “reflinks” on some systems) instead of copying files to the new cluster. This can result in near-instantaneous copying of the data files, giving the speed advantages of -k
/--link
while leaving the old cluster untouched.
File cloning is only supported on some operating systems and file systems. If it is selected but not supported, the pg_upgrade run will error. At present, it is supported on Linux (kernel 4.5 or later) with Btrfs and XFS (on file systems created with reflink support), and on macOS with APFS.
-?
--help
show help, then exit
這些是使用 pg_upgrade 進行升級的步驟:
(選擇性)移動舊叢集目錄
如果你使用的是特定於版本的安裝目錄,例如 /opt/PostgreSQL/13,則毋須移動舊叢集。圖形安裝程序均使用特定於版本的安裝目錄。如果您的安裝目錄不是特定於版本的,例如 /usr/local/pgsql,則必須移動目前的 PostgreSQL 安裝目錄,以免干擾新的 PostgreSQL 安裝。一旦關閉目前的 PostgreSQL 伺服器,就可以重新命名 PostgreSQL 安裝目錄了。假設舊目錄為 /usr/local/pgsql,則可以執行以下操作:
重新命名該目錄。
對於以原始碼安裝的使用者,請編譯新的版本
使用與舊叢集相容的 configure 選項編譯新的 PostgreSQL 原始碼。pg_upgrade 將在開始升級之前檢查 pg_controldata 以確保所有設定是相容的。
安裝新的 PostgreSQL 編譯後可執行檔案
Install the new server's binaries and support files. pg_upgrade is included in a default installation.
For source installs, if you wish to install the new server in a custom location, use the prefix
variable:
初始化新的 PostgreSQL 叢集
Initialize the new cluster using initdb
. Again, use compatible initdb
flags that match the old cluster. Many prebuilt installers do this step automatically. There is no need to start the new cluster.
安裝自訂的共享物件檔案
Install any custom shared object files (or DLLs) used by the old cluster into the new cluster, e.g., pgcrypto.so
, whether they are from contrib
or some other source. Do not install the schema definitions, e.g., CREATE EXTENSION pgcrypto
, because these will be upgraded from the old cluster. Also, any custom full text search files (dictionary, synonym, thesaurus, stop words) must also be copied to the new cluster.
調整身份認證
停止兩個伺服器服務
Make sure both database servers are stopped using, on Unix, e.g.:
or on Windows, using the proper service names:
Streaming replication and log-shipping standby servers can remain running until a later step.
準備備用伺服器的升級
執行 pg_upgrade
Always run the pg_upgrade binary of the new server, not the old one. pg_upgrade requires the specification of the old and new cluster's data and executable (bin
) directories. You can also specify user and port values, and whether you want the data files linked or cloned instead of the default copy behavior.
If you use link mode, the upgrade will be much faster (no file copying) and use less disk space, but you will not be able to access your old cluster once you start the new cluster after the upgrade. Link mode also requires that the old and new cluster data directories be in the same file system. (Tablespaces and pg_wal
can be on different file systems.) Clone mode provides the same speed and disk space advantages but does not cause the old cluster to be unusable once the new cluster is started. Clone mode also requires that the old and new data directories be in the same file system. This mode is only available on certain operating systems and file systems.
The --jobs
option allows multiple CPU cores to be used for copying/linking of files and to dump and reload database schemas in parallel; a good place to start is the maximum of the number of CPU cores and tablespaces. This option can dramatically reduce the time to upgrade a multi-database server running on a multiprocessor machine.
For Windows users, you must be logged into an administrative account, and then start a shell as the postgres
user and set the proper path:
and then run pg_upgrade with quoted directories, e.g.:
Once started, pg_upgrade
will verify the two clusters are compatible and then do the upgrade. You can use pg_upgrade --check
to perform only the checks, even if the old server is still running. pg_upgrade --check
will also outline any manual adjustments you will need to make after the upgrade. If you are going to be using link or clone mode, you should use the option --link
or --clone
with --check
to enable mode-specific checks. pg_upgrade
requires write permission in the current directory.
Obviously, no one should be accessing the clusters during the upgrade. pg_upgrade defaults to running servers on port 50432 to avoid unintended client connections. You can use the same port number for both clusters when doing an upgrade because the old and new clusters will not be running at the same time. However, when checking an old running server, the old and new port numbers must be different.
升級串流複寫和日誌轉送的備用伺服器
If you did not use link mode, do not have or do not want to use rsync, or want an easier solution, skip the instructions in this section and simply recreate the standby servers once pg_upgrade completes and the new primary is running.
Install the new PostgreSQL binaries on standby servers
Make sure the new binaries and support files are installed on all standby servers.
Make sure the new standby data directories do _not_** exist**
Make sure the new standby data directories do not exist or are empty. If initdb was run, delete the standby servers' new data directories.
Install custom shared object files
Install the same custom shared object files on the new standbys that you installed in the new primary cluster.
Stop standby servers
If the standby servers are still running, stop them now using the above instructions.
Save configuration files
Save any configuration files from the old standbys' configuration directories you need to keep, e.g., postgresql.conf
(and any files included by it), postgresql.auto.conf
, pg_hba.conf
, because these will be overwritten or removed in the next step.
Run rsync
When using link mode, standby servers can be quickly upgraded using rsync. To accomplish this, from a directory on the primary server that is above the old and new database cluster directories, run this on the primary for each standby server:
where old_cluster
and new_cluster
are relative to the current directory on the primary, and remote_dir
is above the old and new cluster directories on the standby. The directory structure under the specified directories on the primary and standbys must match. Consult the rsync manual page for details on specifying the remote directory, e.g.,
You can verify what the command will do using rsync's --dry-run
option. While rsync must be run on the primary for at least one standby, it is possible to run rsync on an upgraded standby to upgrade other standbys, as long as the upgraded standby has not been started.
What this does is to record the links created by pg_upgrade's link mode that connect files in the old and new clusters on the primary server. It then finds matching files in the standby's old cluster and creates links for them in the standby's new cluster. Files that were not linked on the primary are copied from the primary to the standby. (They are usually small.) This provides rapid standby upgrades. Unfortunately, rsync needlessly copies files associated with temporary and unlogged tables because these files don't normally exist on standby servers.
If you have tablespaces, you will need to run a similar rsync command for each tablespace directory, e.g.:
If you have relocated pg_wal
outside the data directories, rsync must be run on those directories too.
Configure streaming replication and log-shipping standby servers
Configure the servers for log shipping. (You do not need to run pg_start_backup()
and pg_stop_backup()
or take a file system backup as the standbys are still synchronized with the primary.)
恢復 pg_hba.conf
If you modified pg_hba.conf
, restore its original settings. It might also be necessary to adjust other configuration files in the new cluster to match the old cluster, e.g., postgresql.conf
(and any files included by it), postgresql.auto.conf
.
啟動新的伺服器
The new server can now be safely started, and then any rsync'ed standby servers.
升級後處理程序
If any post-upgrade processing is required, pg_upgrade will issue warnings as it completes. It will also generate script files that must be run by the administrator. The script files will connect to each database that needs post-upgrade processing. Each script should be run using:
The scripts can be run in any order and can be deleted once they have been run.
Caution
In general it is unsafe to access tables referenced in rebuild scripts until the rebuild scripts have run to completion; doing so could yield incorrect results or poor performance. Tables not referenced in rebuild scripts can be accessed immediately.
統計資訊
Because optimizer statistics are not transferred by pg_upgrade
, you will be instructed to run a command to regenerate that information at the end of the upgrade. You might need to set connection parameters to match your new cluster.
刪除舊的叢集檔案
Once you are satisfied with the upgrade, you can delete the old cluster's data directories by running the script mentioned when pg_upgrade
completes. (Automatic deletion is not possible if you have user-defined tablespaces inside the old data directory.) You can also delete the old installation directories (e.g., bin
, share
).
還原到舊的叢集
If, after running pg_upgrade
, you wish to revert to the old cluster, there are several options:
If the --check
option was used, the old cluster was unmodified; it can be restarted.
If the --link
option was not used, the old cluster was unmodified; it can be restarted.
If the --link
option was used, the data files might be shared between the old and new cluster:
If pg_upgrade
aborted before linking started, the old cluster was unmodified; it can be restarted.
If you did not start the new cluster, the old cluster was unmodified except that, when linking started, a .old
suffix was appended to $PGDATA/global/pg_control
. To reuse the old cluster, remove the .old
suffix from $PGDATA/global/pg_control
; you can then restart the old cluster.
If you did start the new cluster, it has written to shared files and it is unsafe to use the old cluster. The old cluster will need to be restored from backup in this case.
pg_upgrade 在目前工作目錄中建立各種工作檔案,例如資料庫結構轉存。為了安全起見,請確保該目錄不能被任何其他使用者讀取或寫入。
pg_upgrade 在舊的和新的資料目錄中啟動短暫的 postmasters。預設情況下,與這些 postmasters 進行通訊的 Unix socket 檔案是在目前工作目錄中建立的。在某些情況下,目前目錄的路徑名稱可能會太長而無法成為有效的 Unix socket 名稱。在這種情況下,可以使用 -s 選項將 Unix socket 檔案放在路徑名稱較短的某個目錄中。為了安全起見,請確保該目錄不能被任何其他使用者讀取或寫入。 (Windows 不支援此功能。)
所有失敗、重建和重新索引的失敗影響到安裝情況,將由 pg_upgrade 回報。用於重建資料表和索引的升級後腳本將自動產生。如果要自動執行許多叢集的升級,則應該發現具有相同資料庫架構的叢集對於所有叢集升級都需要相同的升級後步驟; 這是因為升級後的步驟基於資料庫結構,而不是使用者的資料。
所以為了進行部署測試,請建立舊叢集僅含結構的副本,插入虛擬資料,然後進行升級測試。
pg_upgrade 不支援使用以下 reg* OID 引用系統資料型別的資料表欄位的資料庫:
(regclass
, regrole
, and regtype
can be upgraded.)
If you are upgrading a pre-PostgreSQL 9.2 cluster that uses a configuration-file-only directory, you must pass the real data directory location to pg_upgrade, and pass the configuration directory location to the server, e.g., -d /real-data-directory -o '-D /configuration-directory'
.
If using a pre-9.1 old server that is using a non-default Unix-domain socket directory or a default that differs from the default of the new cluster, set PGHOST
to point to the old server's socket location. (This is not relevant on Windows.)
initdb — 建立一個新的 PostgreSQL 資料庫叢集
initdb
[option
...] [ --pgdata
| -D
] directory
initdb 建立一個新的 PostgreSQL 資料庫叢集。資料庫叢集是以單一個伺服器實例管理許多資料庫的單位。
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
.
伺服器啟動以後,可以變更其餘的區域設定類別。您也可以使用 --locale 來設定所有語言環境類別的預設值,包括排序規則和字元集。可以透過 SHOW ALL 顯示所有伺服器區域設定值(lc_ *)。更多細節可以在 中找到。
要變更預設編碼,請使用 --encoding。更多細節可以在 中找到。
-A
authmethod
--auth=
authmethod
此選項為 pg_hba.conf 中使用的本地使用者(host 和 local 項目)指定預設的身份驗證方法。initdb 將使用指定的身份驗證方法預先設定 pg_hba.conf 的項目,不論是非複寫使用者或是可複寫使用者。
除非您信任系統上的所有本地使用者,否則不要使用 trust。trust 是用於簡易安裝的預設選項。
--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
此選項指定資料庫叢集應儲存的目錄。這是 initdb 唯一需要的資訊,但是您可以避免使用這個選項,而是透過使用 PGDATA 環境變數來設定它。這很方便,因為資料庫伺服器(postgres)以後可以透過相同的變數找到資料庫目錄。
-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
-U
username
--username=
username
選擇資料庫超級使用者的使用者名稱。預設為執行 initdb 的有效使用者的名稱。超級使用者名稱到底是什麼並不重要,也可以選擇保留慣用名稱 postgres,即使與作業系統使用者名稱不同也沒有關係。
-W
--pwprompt
使 initdb 提示輸入密碼以授予資料庫超級使用者。如果您不打算使用密碼身份驗證,那麼這並不重要。否則,在設定密碼之前,您將無法使用密碼身份驗證。
-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
指定 initdb 應該在哪裡找到其輸入檔案以初始化資料庫叢集。通常這不是必須的。系統將告知您是否需要明確指定其路徑。
-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
也可以透過 pg_ctl initdb
呼叫 initdb
。
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 ).
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
-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
-?
--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.
pg_upgrade
will connect to the old and new servers several times, so you might want to set authentication to peer
in pg_hba.conf
or use a ~/.pgpass
file (see ).
If you are upgrading standby servers using methods outlined in section , verify that the old standby servers are caught up by running pg_controldata against the old primary and standby clusters. Verify that the “Latest checkpoint location” values match in all clusters. (There will be a mismatch if old standby servers were shut down before the old primary or if the old standby servers are still running.) Also, make sure wal_level
is not set to minimal
in the postgresql.conf
file on the new primary cluster.
If an error occurs while restoring the database schema, pg_upgrade
will exit and you will have to revert to the old cluster as outlined in below. To try pg_upgrade
again, you will need to modify the old cluster so the pg_upgrade schema restore succeeds. If the problem is a contrib
module, you might need to uninstall the contrib
module from the old cluster and install it in the new cluster after the upgrade, assuming the module is not being used to store user data.
If you used link mode and have Streaming Replication (see ) or Log-Shipping (see ) standby servers, you can follow these steps to quickly upgrade them. You will not be running pg_upgrade on the standby servers, but rather rsync on the primary. Do not start any servers yet.
If you want to use link mode and you do not want your old cluster to be modified when the new cluster is started, consider using the clone mode. If that is not available, make a copy of the old cluster and upgrade that in link mode. To make a valid copy of the old cluster, use rsync
to create a dirty copy of the old cluster while the server is running, then shut down the old server and run rsync --checksum
again to update the copy with any changes to make it consistent. (--checksum
is necessary because rsync
only has file modification-time granularity of one second.) You might want to exclude some files, e.g., postmaster.pid
, as documented in . If your file system supports file system snapshots or copy-on-write file copies, you can use that to make a backup of the old cluster and tablespaces, though the snapshot and copies must be created simultaneously or while the database server is down.
, , ,
選擇樣板資料庫的編碼。這也是以後建立的任何資料庫的預設編碼,除非您在此處覆蓋它。預設值是從區域設定產生的;如果不起作用,則會回到 SQL_ASCII。PostgreSQL 伺服器支援的字元集在中說明。
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.
設定資料庫叢集的預設語言環境。如果未指定此選項,則語言環境將從 initdb 所執行的環境繼承。語言環境支援在 中的說明描述。
Sets the default text search configuration. See for further information.
指定要建立的資料庫叢集的預設時區。該值應為完整的時區名稱(請參閱)。
與大多數其他 PostgreSQL 工具程式一樣,此工具程式也使用 libpq 所支援的環境變數(請參閱)。
,
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 for discussion.
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 for discussion.
Simon Riggs <
>
|
|
|
|
|
|
|
|