Postgresql 中文操作指南
Description
postgres 是 PostgreSQL 数据库服务器。为了让客户端应用程序访问数据库,它连接(通过网络或本地)到正在运行的 postgres 实例。然后, postgres 实例启动单独的服务器进程来处理连接。
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.
一个 postgres 实例始终管理恰好一个数据库集群的数据。数据库集群是由存储在公共文件系统位置(“数据区”)中的数据库的集合。一个系统上可以同时运行多个 postgres 实例,只要它们使用不同的数据区和不同的通信端口(参见下文)。当 postgres 启动时,它需要知道数据区的。位置必须由 -D 选项或 PGDATA 环境变量指定;没有默认设置。通常, -D 或 PGDATA 直接指向由 initdb 创建的数据区目录。其他可能的文件布局在 Section 20.2 中进行讨论。
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 20.2.
默认情况下, postgres 在前台启动,并向标准错误流打印日志消息。在实际应用程序中,应该后台进程的方式启动 postgres ,可能在启动时启动。
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.
postgres 命令可以在单用户模式下调用。此模式的主要用途是在 initdb 引导期间。有时它被用于调试或灾难恢复;请注意,运行单用户服务器并不真正适合调试服务器,因为不会进行实际进程间通信和锁定。当从 Shell 中以单用户模式调用时,用户可以输入查询,结果将打印到屏幕,但以对开发人员比最终用户更有用的格式打印。在单用户模式中,会话用户将设置为具有 ID 1 的用户,并且会向此用户授予隐式超级用户权限。此用户实际上不必存在,因此可以用单用户模式从某些类型的意外损坏中手动恢复系统目录。
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.
Options
postgres 接受以下命令行参数。有关各个选项的详细讨论,请参阅 Chapter 20 。你可以通过设置配置文件来免去键入大多数这些选项的麻烦。某些(安全)选项还可以从连接客户端以特定于应用程序的方式设置,以仅用于该会话。例如,如果环境变量 PGOPTIONS 已设置,那么基于 libpq 的客户端会将该字符串传递给服务器,服务器会将它解释为 postgres 命令行选项。
postgres accepts the following command-line arguments. For a detailed discussion of the options consult Chapter 20. 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.
General Purpose
-
-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 20. 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 returns values from postgresql.conf, modified by any parameters supplied in this invocation. It does not reflect parameters supplied when the cluster was started.
-
This can be used on a running server for most parameters. However, the server must be shut down for some runtime-computed parameters (e.g., shared_memory_size, shared_memory_size_in_huge_pages, and wal_segment_size).
-
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 use SHOW 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 20.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 fsync configuration 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 19.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.
-
-
-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 base amount of memory to be used by sorts and hash tables before resorting to temporary disk files. See the description of the work_mem configuration parameter in Section 20.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.
-
Semi-Internal Options
此处描述的选项主要用于调试目的,在某些情况下可协助恢复严重损坏的数据库。在生产数据库设置中没有使用它们的理由。在此处列出它们只是供 PostgreSQL 系统开发者使用。此外,在不发出通知的情况下,这些选项在将来的发行版中可能会更改或删除。
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.
-
-
-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, by sending them SIGQUIT signals. With this option, SIGABRT will be sent instead, resulting in production of core dump files.
-
-
-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.
-
Options for Single-User Mode
以下选项仅适用于单用户模式(参见下文 Single-User Mode )。
The following options only apply to the single-user mode (see Single-User Mode below).
-
—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.
-
Environment
-
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)
-
Diagnostics
提及 semget 或 shmget 的失败消息可能表明您需要配置内核以提供足够的共享内存和信号量。有关更多讨论,请参见 Section 19.4 。您也许可以通过降低 shared_buffers 以减少 PostgreSQL 的共享内存消耗,和/或降低 max_connections 以减少信号量消耗,来延迟重新配置内核。
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 19.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
$ ps ax | grep postgres
或
or
$ ps -ef | grep postgres
这取决于你的系统。如果你确定没有任何冲突的服务器正在运行,则可以删除该消息中提到的锁文件并重试。
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.
指示无法绑定到端口的错误消息可能表示该端口已被某些非 PostgreSQL 进程使用。如果终止 postgres 并立即使用相同的端口重新启动它,你也会收到此错误;在这种情况下,你只需等待几秒钟直到操作系统关闭端口再重试即可。最后,如果你指定操作系统认为是保留的端口号,也会收到此错误。例如,许多版本的 Unix 认为端口号低于 1024 是“可信”的,并且只允许 Unix 超级用户访问它们。
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.
Notes
实用程序命令 pg_ctl 可用于安全方便地启动和关闭 postgres 服务器。
The utility command pg_ctl can be used to start and shut down the postgres server safely and comfortably.
如果可能, do not 使用 SIGKILL 来关闭主 postgres 服务器。这样做将阻止 postgres 在终止前释放它所保持的系统资源(例如,共享内存和信号量)。这可能会导致启动新的 postgres 运行时出现问题。
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.
要正常终止 postgres 服务器,可以使用信号 SIGTERM 、 SIGINT 或 SIGQUIT 。第一个信号将在所有客户端终止前等待,第二个信号将强制断开所有客户端,第三个信号将立即退出而不进行适当的关闭,从而在重新启动期间导致恢复运行。
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.
SIGHUP 信号将重新加载服务器配置文件。也可以将 SIGHUP 发送到单独的服务器进程,但这通常是不明智的。
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.
要取消正在运行的查询,请向运行该命令的进程发送 SIGINT 信号。要干净地终止后端进程,请向该进程发送 SIGTERM 。另请参见 Section 9.27.2 中的 pg_cancel_backend 和 pg_terminate_backend ,它们是这两种操作的 SQL 可调用等效项。
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.27.2 for the SQL-callable equivalents of these two actions.
postgres 服务器使用 SIGQUIT 来指示从属服务器进程在未进行正常清理的情况下终止。此信号 should not 可由用户使用。向服务器进程发送 SIGKILL 也是不明智的——主 postgres 进程会将此解释为崩溃,并将强制所有兄弟进程作为其标准崩溃恢复程序的一部分退出。
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.
Bugs
— 选项在 FreeBSD 或 OpenBSD 上不起作用。请改用 -c 。这是受影响操作系统的错误;如果此错误未得到修复,PostgreSQL 的未来版本将提供变通方法。
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.
Single-User Mode
要启动单用户模式服务器,请使用以下命令:
To start a single-user mode server, use a command like
postgres --single -D /usr/local/pgsql/data other-options my_database
使用 -D 提供数据库目录的正确路径,或确保已设置环境变量 PGDATA 。还要指定你想要使用的特定数据库的名称。
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.
通常,单用户模式服务器将换行符视为命令输入终止符;与 psql 中一样,这里没有有关分号的智能信息。要跨多行继续命令,你必须在每个换行符之前(最后一行的除外)键入反斜杠。反斜杠和相邻的换行符都从输入命令中删除。请注意,即使在字符串文本或注释中,也会发生这种情况。
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.
但是,如果你使用 -j 命令行开关,则单个换行符不会终止命令输入;而是分号-换行符-换行符序列终止。也就是说,立即键入一个分号,后跟一个完全空的行。在此模式中,不会对反斜杠-换行符进行特殊处理。同样,也没有有关此类序列出现在字符串字面或注释中的智能信息。
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.
要退出会话,请键入 EOF ( Control + D ,通常为)。如果你在最后一个命令输入终止符后输入了任何文本,则 EOF 将被视为命令输入终止符,并且需要另一个 EOF 才能退出。
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.
Examples
要在后台使用默认值启动 postgres ,请键入:
To start postgres in the background using default values, type:
$ nohup postgres >logfile 2>&1 </dev/null &
要使用特定端口启动 postgres ,例如,1234:
To start postgres with a specific port, e.g., 1234:
$ postgres -p 1234
要使用 psql 连接到此服务器,请使用 -p 选项指定此端口:
To connect to this server using psql, specify this port with the -p option:
$ psql -p 1234
或设置环境变量 PGPORT :
or set the environment variable PGPORT:
$ export PGPORT=1234
$ psql
命名运行时参数可通过以下任意一种方式设置:
Named run-time parameters can be set in either of these styles:
$ postgres -c work_mem=1234
$ postgres --work-mem=1234
任何形式都将覆盖 postgresql.conf 中 work_mem 所做的任何设置。请注意,参数名称中的下划线可在命令行中写成下划线或破折号。除了短期实验,可能最好在 postgresql.conf 中编辑设置,而不是依赖于命令行开关设置参数。
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.