签到成功

知道了

CNDBA社区CNDBA社区

PostgreSQL 学习笔记(4) -- PG 启动 与 关闭

2022-12-05 22:56 1726 0 原创 PostgreSQL
作者: dave

在之前的博客,我们了解的PG的相关信息,如下:https://www.cndba.cn/dave/article/116375

PostgreSQL 学习笔记(1) — PG 概述
https://www.cndba.cn/dave/article/116370

PostgreSQL 学习笔记(2) — PG 版本发布策略 和 生命周期说明
https://www.cndba.cn/dave/article/116372

https://www.cndba.cn/dave/article/116375

PostgreSQL 学习笔记(3) — PG 单实例安装手册
https://www.cndba.cn/dave/article/116374

本片我们了解下PG的启停和访问。

https://www.cndba.cn/dave/article/116375
https://www.cndba.cn/dave/article/116375

1 配置环境变量

我们在上节使用默认的方式安装了PG。

软件默认在/usr/pgsql-14路径下:

https://www.cndba.cn/dave/article/116375

[dave@www.cndba.cn ~]# cd /usr/pgsql-14/
[dave@www.cndba.cn pgsql-14]# ll
total 16
drwxr-xr-x. 2 root root 4096 Aug  8 09:41 bin
drwxr-xr-x. 3 root root   23 Aug  8 09:41 doc
drwxr-xr-x. 3 root root 4096 Aug  8 09:41 lib
drwxr-xr-x. 8 root root 4096 Aug  8 09:41 share
[dave@www.cndba.cn pgsql-14]#

Initdb 也是使用的默认配置,所以在/var/lib/pgsql/14 目录下生成了相关文件:

[dave@www.cndba.cn 14]# pwd
/var/lib/pgsql/14
[dave@www.cndba.cn 14]# ll
total 8
drwx------.  2 postgres postgres    6 Nov 10  2022 backups
drwx------. 20 postgres postgres 4096 Aug  8 09:47 data
-rw-------.  1 postgres postgres  921 Aug  8 09:47 initdb.log
[dave@www.cndba.cn 14]#

在实际生产中,在初始化实例时可以使用initdb或者pg_ctl命令指定到规划的目录,比如:

https://www.cndba.cn/dave/article/116375

initdb -D /pgdata/14/data -W
pg_ctl init -D /pgdata/14/data -o

我们这里按默认的配置,在/etc/profile 中添加如下几行:

https://www.cndba.cn/dave/article/116375

export PATH=/usr/pgsql-14/bin:$PATH
export LD_LIBRARY_PATH=/usr/pgsql-14/lib:$LD_LIBRARY_PATH
export PGDATA=/var/lib/pgsql/14/data

2 使用 systemctl 命令

使用rpm 或则 yum 安装的pg,默认会创建服务,我们可以直接使用systtemctl 命令来起停PG。https://www.cndba.cn/dave/article/116375

[dave@www.cndba.cn lib]# cd /usr/lib/systemd/system
[dave@www.cndba.cn system]# ll post
postfix.service        postgresql-14.service
[dave@www.cndba.cn system]# ll postgresql-14.service
-rw-r--r--. 1 root root 1764 Nov 10  2022 postgresql-14.service
[dave@www.cndba.cn system]#

配置PG 开机自启动:https://www.cndba.cn/dave/article/116375

[dave@www.cndba.cn system]# systemctl disable postgresql-14.service
Removed symlink /etc/systemd/system/multi-user.target.wants/postgresql-14.service.

[dave@www.cndba.cn system]# systemctl enable postgresql-14.service
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-14.service to /usr/lib/systemd/system/postgresql-14.service.
[dave@www.cndba.cn system]#

关闭、启动数据库:

[dave@www.cndba.cn system]# systemctl stop postgresql-14.service
[dave@www.cndba.cn system]# systemctl start postgresql-14.service

[dave@www.cndba.cn system]# ps -ef|grep pg
postgres 13968     1  0 13:39 ?        00:00:00 /usr/pgsql-14/bin/postmaster -D /var/lib/pgsql/14/data/
root     13988 11272  0 13:39 pts/0    00:00:00 grep --color=auto pg
[dave@www.cndba.cn system]#

3 使用 pg_ctl 命令

pg_ctl 命令为 PostgreSQL 服务端应用程序,可以用来初始化,启动和停止及控制 PostgreSQL 服务器。

pg_ctl 命令必须使用postgres 用户执行,root 执行会报如下错误:

https://www.cndba.cn/dave/article/116375

[dave@www.cndba.cn ~]# pg_ctl restart -D /var/lib/pgsql/14/data/
pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.
[dave@www.cndba.cn ~]#

3.1 pg_ctl 命令说明

pg_ctl 语法格式:

初始化数据库
pg_ctl init[db]   [-D DATADIR] [-s] [-o OPTIONS]

启动数据库
pg_ctl start      [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s] [-o OPTIONS] [-p PATH] [-c]

关闭数据库
pg_ctl stop       [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]

重启数据库
pg_ctl restart    [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s] [-o OPTIONS] [-c]

重新加载 postgresql.conf 或 pg_hba.conf 文件
pg_ctl reload     [-D DATADIR] [-s]

查看服务器是否在指定的数据目录运行
pg_ctl status     [-D DATADIR]
pg_ctl promote    [-D DATADIR] [-W] [-t SECS] [-s]
pg_ctl logrotate  [-D DATADIR] [-s]
pg_ctl kill       SIGNALNAME PID

命令选项
-D, --pgdata=DATADIR:指定数据库相关文件的数据目录,如果省略,默认读取 PGDATA 环境变量
-s, --silent:静默输出,仅仅输出错误消息
-t, --timeout=SECS:指定等待操作完成的最大延时秒数。默认为 PGCTLTIMEOUT 环境变量的值,如果省略,默认60秒
-V, --version          output version information, then exit
-w, --wait:等待操作完成,如果操作在延迟时间内未完成,pg_ctl 退出状态为非零
-W, --no-wait:不等待操作完成,不会提示数据库停止是否完成
-c, --core-files       allow postgres to produce core files
-l, --log=FILENAME:将服务器日志输出追加到 filename中,也叫做服务器日志文件。如果该文件的 umask 设置为077,访问日志文件默认情况下其它用户不可读。
-o, --options=OPTIONS  command line options to pass to postgres (PostgreSQL server executable) or initdb
-p PATH-TO-POSTGRES    normally not necessary
-m, --mode=MODE:指定关闭数据库的模式,有三个选项,smart,fast,immediate,省略默认为fast
smart:smart模式会等待活动的事务提交结束, 并等待客户端主动断开连接之后关闭数据库 
fast:fast模式则会回滚所有活动的事务, 并强制断开客户端的连接之后关闭数据库(默认)
immediate:模式立即终止所有服务器进程,当下一次数据库启动时它会首先进入恢复状态(不推荐使用)

3.2 操作示例

[dave@www.cndba.cn ~]# su - postgres
Last login: Mon Aug  8 13:31:29 CST 2022 on pts/2

-bash-4.2$ pg_ctl restart -D /var/lib/pgsql/14/data/
waiting for server to shut down.... done
server stopped
waiting for server to start....2022-08-08 13:52:14.203 CST [14887] LOG:  redirecting log output to logging collector process
2022-08-08 13:52:14.203 CST [14887] HINT:  Future log output will appear in directory "log".
 done
server started

-bash-4.2$ pg_ctl stop -D /var/lib/pgsql/14/data/
waiting for server to shut down.... done
server stopped

-bash-4.2$ ps -ef|grep pg
postgres 14911 14817  0 13:52 pts/3    00:00:00 grep --color=auto pg

-bash-4.2$ pg_ctl start -D /var/lib/pgsql/14/data/
waiting for server to start....2022-08-08 13:52:35.544 CST [14918] LOG:  redirecting log output to logging collector process
2022-08-08 13:52:35.544 CST [14918] HINT:  Future log output will appear in directory "log".
 done
server started

-bash-4.2$ ps -ef|grep pg
postgres 14918     1  0 13:52 ?        00:00:00 /usr/pgsql-14/bin/postgres -D /var/lib/pgsql/14/data
postgres 14931 14817  0 13:52 pts/3    00:00:00 grep --color=auto pg
-bash-4.2$

版权声明:本文为博主原创文章,未经博主允许不得转载。

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
dave

dave

关注

人的一生应该是这样度过的:当他回首往事的时候,他不会因为虚度年华而悔恨,也不会因为碌碌无为而羞耻;这样,在临死的时候,他就能够说:“我的整个生命和全部精力,都已经献给世界上最壮丽的事业....."

  • 2262
    原创
  • 3
    翻译
  • 578
    转载
  • 192
    评论
  • 访问:8080943次
  • 积分:4349
  • 等级:核心会员
  • 排名:第1名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ