2022PostgreSQL服务器启动及关闭方法PostgreSQL服务器启动及关闭方法PostgreSQL采纳C/S(客户机/服务器)模式结构。应用层通过INET或者UnixSocket利用既定的协议与数据库服务器进行通信。下面yjbys我为大家搜集相关的PostgreSQL服务器启动及关闭方法!1.启动数据库服务器(posgres用户):[postgres@localhostbin]$postgres-D/opt/postgresql/data/>/opt/postgresql/log/pg_server.log第1页共11页2>&1&[1]4508当然假如设置了环境变量PGDATA=/opt/postgresql/dataexportPGDATA后,可运用pg_ctl工具进行启动:[postgres@localhostlog]$pg_ctlstart-l/opt/postgresql/log/pg_server.logpg_ctl:anotherservermightberunning;tryingtostart第2页共11页serveranywaypg_ctl:couldnotstartserverExaminethelogoutput.[postgres@localhostlog]$因为之前已经启动,所以打印;anotherservermightberunning;。此时,查看日志,有如下信息:[postgres@localhostlog]$catpg_server.logFATAL:lockfile"postmaster.pid"alreadyexists第3页共11页HINT:Isanotherpostmaster(PID4491)runningindatadirectory"/opt/postgresql/data"?[postgres@localhostlog]$当然,最简的.启动方式是:[postgres@localhost~]$pg_ctlstartserverstarting[postgres@localhost~]$LOG:databasesystemwasshutdownat2022-07-0913:58:00CSTLOG:autovacuumlauncherstarted第4页共11页LOG:databasesystemisreadytoacceptconnections假如要在操作系统启动时就启动PG,可以在/etc/rc.d/rc.local文件中加以下语句:/opt/postgresql/bin/pg_ctlstart-l/opt/postgresql/log/pg_server.log-D/opt/postgresql/data2.关闭服务器最简洁方法:[postgres@localhost~]$pg_ctlstopwaitingforservertoshutdown....done第5页共11页serverstopped与Oracle相同,在关闭时也可采纳不同的模式,简介如下:SIGTERM不再允许新的连接,但是允许全部活跃的会话正常完成他们的工作,只有在全部会话都结束任务后才关闭。这是智能关闭。SIGINT不再允许新的连接,向全部活跃服务器发送SIGTERM(让它们立即退出),然后等待全部子进程退出并关闭数据库。这是快速关闭。SIGQUIT第6页共11页令postgres向全部子进程发送SIGQUIT并且马上退出(全部子进程也会马上退出),而不会妥当地关闭数据库系统。这是马上关闭。这样做会导致下次启动时的复原(通过重放WAL日志)。我们举荐只在紧急的时候运用这个方法。SIGKILL此选项尽量不要运用,这样会阻挡服务器清理共享内存和信号灯资源,那样的话你只能在启动服务器之前自己手工做这件事。另外,SIGKILL干脆把postgres杀掉,而不会等它把信号中继给它的子进程,因此我们还须要手工杀掉每个独立子进程。第7页共11页运用方法举例:[postgres@localhost~]$pg_ctlstop-oSIGTERMLOG:receivedsmartshutdownrequestLOG:autovacuumlaunchershuttingdownwaitingforservertoshutdown....LOG:shuttingdownLOG:databasesystemisshutdowndoneserverstopped[postgres@localhost~]$第8页共11页最快速关闭方法:killpostgres进程[postgres@localhost~]$kill-INT`head-1/opt/postgresql/data/postmaster.pid`[postgres@localhost~]$LOG:receivedfastshutdownrequestLOG:abortinganyactivetransactionsLOG:autovacuumlaunchershuttingdownLOG:shuttingdownLOG:databasesystemisshutdown第9页共11页附:postgre启动后的进程,如下:[postgres@localhost~]$ps-ef|greppostroot46094543013:57pts/200:00:00su-postgrespostgres46104609013:57pts/200:00:00-bashpostgres47241014:08pts/200:00:00/opt/postgresql/bin/postgrespostgres47264724014:08?00:00:00postgres:writerprocesspostgres47274724014:08?00:00:00postgres:wal第10页共11页writerprocesspostgres47284724014:08?00:00:00postgres:autovacuumlauncherprocesspostgres47294724014:08?00:00:00postgres:statscollectorprocesspostgres47524610014:11pts/200:00:00ps-efpostgres47534610014:11pts/200:00:00greppost[postgres@localhost~]$本文来源:网络收集与整理,如有侵权,请联系作者删除,谢谢!第11页共11页