Postgresql 中文操作指南
19.3. Starting the Database Server #
在任何人可以访问数据库之前,你必须启动数据库服务器。数据库服务器程序称为 postgres。
Before anyone can access the database, you must start the database server. The database server program is called postgres.
如果你使用的是 PostgreSQL 的预打包版本,则它几乎肯定包含根据操作系统惯例以后台任务形式运行服务器的规定。使用该包的基础架构来启动服务器将比弄清楚如何自己执行此操作轻松得多。有关详细信息,请查阅包级文档。
If you are using a pre-packaged version of PostgreSQL, it almost certainly includes provisions for running the server as a background task according to the conventions of your operating system. Using the package’s infrastructure to start the server will be much less work than figuring out how to do this yourself. Consult the package-level documentation for details.
仅手动启动服务器的方法就是直接调用 postgres,并用 -D 选项指定数据目录的位置,例如:
The bare-bones way to start the server manually is just to invoke postgres directly, specifying the location of the data directory with the -D option, for example:
$ postgres -D /usr/local/pgsql/data
这将使服务器在前台运行。必须在登录到 PostgreSQL 用户帐户时执行此操作。如果没有 -D,服务器将尝试使用环境变量 PGDATA 指定的数据目录。如果也没有提供该变量,则会失败。
which will leave the server running in the foreground. This must be done while logged into the PostgreSQL user account. Without -D, the server will try to use the data directory named by the environment variable PGDATA. If that variable is not provided either, it will fail.
通常,最好在后台启动 postgres。为此,请使用通常的 Unix shell 语法:
Normally it is better to start postgres in the background. For this, use the usual Unix shell syntax:
$ postgres -D /usr/local/pgsql/data >logfile 2>&1 &
以如上所示的方式将服务器的 stdout 和 stderr 输出存储在某处非常重要。这将有助于进行审计和诊断问题。(请参见 Section 25.3 以获得对日志文件操作的更深入讨论。)
It is important to store the server’s stdout and stderr output somewhere, as shown above. It will help for auditing purposes and to diagnose problems. (See Section 25.3 for a more thorough discussion of log file handling.)
postgres 程序还会获取许多其他命令行选项。有关详细信息,请参阅 postgres 参考页和 Chapter 20 (如下)。
The postgres program also takes a number of other command-line options. For more information, see the postgres reference page and Chapter 20 below.
这种 shell 语法很快就变得很繁琐。因此,提供了包装程序 pg_ctl 来简化某些任务。例如:
This shell syntax can get tedious quickly. Therefore the wrapper program pg_ctl is provided to simplify some tasks. For example:
pg_ctl start -l logfile
将启动服务器后台并把输出放入指定日志文件。-D 选项在此的含义与 postgres 相同。pg_ctl 还能够停止服务器。
will start the server in the background and put the output into the named log file. The -D option has the same meaning here as for postgres. pg_ctl is also capable of stopping the server.
通常,您希望在计算机启动时启动数据库服务器。自动启动脚本是操作系统特定的。PostgreSQL 中 contrib/start-scripts 目录中分发了几个示例脚本。安装它们需要 root 权限。
Normally, you will want to start the database server when the computer boots. Autostart scripts are operating-system-specific. There are a few example scripts distributed with PostgreSQL in the contrib/start-scripts directory. Installing one will require root privileges.
不同的系统在启动时间启动守护进程时有不同的约定。许多系统都有文件 /etc/rc.local 或 /etc/rc.d/rc.local。其他系统使用 init.d 或 rc.d 目录。无论您做什么,服务器都必须由 PostgreSQL 用户账户 and not by root 或任何其他用户运行。因此,您可能应该使用 su postgres -c '…' 形成命令。例如:
Different systems have different conventions for starting up daemons at boot time. Many systems have a file /etc/rc.local or /etc/rc.d/rc.local. Others use init.d or rc.d directories. Whatever you do, the server must be run by the PostgreSQL user account and not by root or any other user. Therefore you probably should form your commands using su postgres -c '…'. For example:
su postgres -c 'pg_ctl start -D /usr/local/pgsql/data -l serverlog'
以下是一些更具体的操作系统建议。(在每种情况下,请务必使用我们显示通用值时使用的正确安装目录和用户名。)
Here are a few more operating-system-specific suggestions. (In each case be sure to use the proper installation directory and user name where we show generic values.)
在服务器正在运行时,其 PID 存储在数据目录中的文件 postmaster.pid 中。这用于防止多个服务器实例在同一数据目录中运行,也可以用于关闭服务器。
While the server is running, its PID is stored in the file postmaster.pid in the data directory. This is used to prevent multiple server instances from running in the same data directory and can also be used for shutting down the server.
19.3.1. Server Start-up Failures #
有几个常见的原因会导致服务器无法启动。检查服务器日志文件,或手动启动它(不重定向标准输出或标准错误),看看出现了哪些错误消息。下面我们更详细地解释一些最常见的错误消息。
There are several common reasons the server might fail to start. Check the server’s log file, or start it by hand (without redirecting standard output or standard error) and see what error messages appear. Below we explain some of the most common error messages in more detail.
LOG: could not bind IPv4 address "127.0.0.1": Address already in use
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
FATAL: could not create any TCP/IP sockets
这通常表示它的暗示:您尝试在已经运行一个服务器的同一端口上启动另一个服务器。但是,如果内核错误消息不是 Address already in use 或其一些变体,则可能存在其他问题。例如,尝试在保留端口号上启动服务器可能会绘制如下内容:
This usually means just what it suggests: you tried to start another server on the same port where one is already running. However, if the kernel error message is not Address already in use or some variant of that, there might be a different problem. For example, trying to start a server on a reserved port number might draw something like:
$ postgres -p 666
LOG: could not bind IPv4 address "127.0.0.1": Permission denied
HINT: Is another postmaster already running on port 666? If not, wait a few seconds and retry.
FATAL: could not create any TCP/IP sockets
像这样的消息:
A message like:
FATAL: could not create shared memory segment: Invalid argument
DETAIL: Failed system call was shmget(key=5440001, size=4011376640, 03600).
可能说明你的内核对共享内存大小的限制低于 PostgreSQL 尝试创建的工作区域(在此示例中为 4011376640 字节)。如果你已将 shared_memory_type 设置为 sysv,那么很有可能出现这种情况。在这种情况下,你可以尝试使用少于正常数量的缓冲区 ( shared_buffers) 启动服务器,也可以重新配置内核以增加允许的共享内存大小。如果你尝试在同一台计算机上启动多个服务器,而它们的总请求空间超过了内核限制,那么你可能还会看到此消息。
probably means your kernel’s limit on the size of shared memory is smaller than the work area PostgreSQL is trying to create (4011376640 bytes in this example). This is only likely to happen if you have set shared_memory_type to sysv. In that case, you can try starting the server with a smaller-than-normal number of buffers (shared_buffers), or reconfigure your kernel to increase the allowed shared memory size. You might also see this message when trying to start multiple servers on the same machine, if their total space requested exceeds the kernel limit.
像这样的错误:
An error like:
FATAL: could not create semaphores: No space left on device
DETAIL: Failed system call was semget(5440126, 17, 03600).
表示你用完了磁盘空间。它说明你的内核对 System V 信号量的数量限制低于 PostgreSQL 要创建的数量。如上所述,你可以通过使用减少的允许连接数量 ( max_connections) 来启动服务器来解决此问题,但是最终你仍需要增加内核限制。
does not mean you’ve run out of disk space. It means your kernel’s limit on the number of System V semaphores is smaller than the number PostgreSQL wants to create. As above, you might be able to work around the problem by starting the server with a reduced number of allowed connections (max_connections), but you’ll eventually want to increase the kernel limit.
有关如何配置 System V IPC 设施的详细信息,请参见 Section 19.4.1。
Details about configuring System V IPC facilities are given in Section 19.4.1.
19.3.2. Client Connection Problems #
尽管客户端端的可能错误条件多种多样,并且依赖于应用程序,但其中一些可能直接与服务器的启动方式相关。除以下所示以外的条件应记录在各自的客户端应用程序中。
Although the error conditions possible on the client side are quite varied and application-dependent, a few of them might be directly related to how the server was started. Conditions other than those shown below should be documented with the respective client application.
psql: error: connection to server at "server.joe.com" (123.123.123.123), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
这是一个通用的“找不到可以对话的服务器”故障。在尝试 TCP/IP 通信时,它看起来像上面。一个常见的错误是忘记配置服务器以允许 TCP/IP 连接。
This is the generic “I couldn’t find a server to talk to” failure. It looks like the above when TCP/IP communication is attempted. A common mistake is to forget to configure the server to allow TCP/IP connections.
或者,在尝试与本地服务器进行 Unix 域套接字通信时您可能会收到此错误:
Alternatively, you might get this when attempting Unix-domain socket communication to a local server:
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
如果服务器确实正在运行,请检查客户端对套接字路径(此处为 /tmp)的设想是否与服务器的 unix_socket_directories 设置相一致。
If the server is indeed running, check that the client’s idea of the socket path (here /tmp) agrees with the server’s unix_socket_directories setting.
连接失败消息始终会显示服务器地址或套接字路径名称,这有助于验证客户端是否正在尝试连接到正确的位置。如果那里确实没有服务器正在侦听,则内核错误消息通常会是 Connection refused 或 No such file or directory,如示例中所示。(在此上下文中,意识到 Connection refused 并 not 不意味着服务器收到你的连接请求并拒绝该请求很重要。此情况将生成另一条消息,如 Section 21.15 中所示。)其他错误消息(例如 Connection timed out)可能表明存在更根本性的问题,例如缺乏网络连接性,或防火墙阻止了连接。
A connection failure message always shows the server address or socket path name, which is useful in verifying that the client is trying to connect to the right place. If there is in fact no server listening there, the kernel error message will typically be either Connection refused or No such file or directory, as illustrated. (It is important to realize that Connection refused in this context does not mean that the server got your connection request and rejected it. That case will produce a different message, as shown in Section 21.15.) Other error messages such as Connection timed out might indicate more fundamental problems, like lack of network connectivity, or a firewall blocking the connection.