PostgreSQL自带的命令行工具24- postgres
postgres
命令主要是 PostgreSQL 数据库服务器的主要执行文件。当你启动 PostgreSQL 服务时,实际上就是在后台运行 postgres
程序。这个程序负责处理用户请求、执行 SQL 命令、管理数据库文件等核心数据库管理任务。通常,你不需要直接运行 postgres
命令,因为有其他工具,如 pg_ctl
、系统服务(例如 systemd 或 init.d),能够更方便地管理 PostgreSQL 服务。
通过help查看帮助文档。
[pg16@test ~]$ postgres --help
postgres is the PostgreSQL server.Usage:postgres [OPTION]...Options:-B NBUFFERS number of shared buffers-c NAME=VALUE set run-time parameter-C NAME print value of run-time parameter, then exit-d 1-5 debugging level-D DATADIR database directory-e use European date input format (DMY)-F turn fsync off-h HOSTNAME host name or IP address to listen on-i enable TCP/IP connections (deprecated)-k DIRECTORY Unix-domain socket location-N MAX-CONNECT maximum number of allowed connections-p PORT port number to listen on-s show statistics after each query-S WORK-MEM set amount of memory for sorts (in kB)-V, --version output version information, then exit--NAME=VALUE set run-time parameter--describe-config describe configuration parameters, then exit-?, --help show this help, then exitDeveloper options:-f s|i|o|b|t|n|m|h forbid use of some plan types-O allow system table structure changes-P disable system indexes-t pa|pl|ex show timings after each query-T send SIGABRT to all backend processes if one dies-W NUM wait NUM seconds to allow attach from a debuggerOptions for single-user mode:--single selects single-user mode (must be first argument)DBNAME database name (defaults to user name)-d 0-5 override debugging level-E echo statement before execution-j do not use newline as interactive query delimiter-r FILENAME send stdout and stderr to given fileOptions for bootstrapping mode:--boot selects bootstrapping mode (must be first argument)--check selects check mode (must be first argument)DBNAME database name (mandatory argument in bootstrapping mode)-r FILENAME send stdout and stderr to given filePlease read the documentation for the complete list of run-time
configuration settings and how to set them on the command line or in
the configuration file.Report bugs to <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>
postgres
命令详解
尽管 postgres
命令通常不直接由用户手动启动,了解其选项可以帮助你理解 PostgreSQL 服务器的工作方式:
postgres [选项...]
主要选项包括:
-D <目录>
或--data-directory=<目录>
:指定 PostgreSQL 数据文件的存储目录。-c <设置项>=<值>
:直接在命令行设置 PostgreSQL 的配置参数。-p <端口号>
或--port=<端口号>
:指定 PostgreSQL 服务器监听的端口号。-l
或--log-statement=all
:记录(日志)每条执行的 SQL 语句,有助于调试。--debug-print-plan
:输出执行计划的详细信息,也是一个调试功能。
启动 PostgreSQL
一种手动启动 PostgreSQL 服务器的方法是直接使用 postgres
命令配合 -D
选项,示例如下:
postgres -D /path/to/your/data/directory
这个命令会启动 PostgreSQL 服务器,并使用指定的数据目录。
注意事项
- 通常情况下,最好使用
pg_ctl
这样的工具来启动、停止或重启 PostgreSQL 服务而不是直接使用postgres
命令。pg_ctl
提供了更多的控制选项并且更适合一般用户。 - 如果你需要手动启动
postgres
命令,确保你以正确的用户身份运行,通常这个用户是安装 PostgreSQL 时创建的postgres
用户。 - 除非你很清楚自己在做什么,否则不推荐在生产环境下直接使用
postgres
命令来管理数据库服务。在生产环境下,数据库服务通常通过系统级的服务管理工具(如 systemd 或 sysvinit)自动启动。
postgres
命令是 PostgreSQL 数据库服务器的核心,虽然日常管理中我们很少直接用到它,但理解其作用和参数选项对于深入理解 PostgreSQL 的运行机制是非常有帮助的。