启动数据库
pg_ctl start -D $PGDATA
其中,环境变量"PGDATA"指向具体的PostgreSQL数据库的数据目录,示例如下:
osdba@osdba-laptop:~$ pg_ctl start -D /home/osdba/pgdata
server starting
停止数据库
pg_ctl stop -D $PGDATA [-m SHUTDOWN-MODE]
其中-m用于指定数据库的停止方法,有以下3种模式:
- smart:等所有连接中止后,关闭数据库。如果客户端连接不终止,则无法关闭数据库。
- fast:快速关闭数据库,断开客户端的连接,让已有的事务回滚,然后正常关闭数据库。相当于Oracle数据库关闭时的immediate模式。
- immediate:立即关闭数据库,相当于数据库进程立即停止,直接退出,下次启动数据库需要进行恢复。相当于Oracle数据库关闭时的abort模式。
PostgreSQL数据库中的immediate关机模式相当于Oracle数据库中的abort关机模式,而Oracle中的immediate关机模式实际上对应的是PostgreSQL中的fast模式,对于从Oracle数据库中转过来的DBA尤其需要注意这一点。
较常用的关闭数据库的方法是fast模式,因为如果采用smart模式,有用户连接到数据库时,系统会一直等待,而无法关闭数据库。PostgreSQL9.5之前的版本默认是smart模式,通常要使用命令“pg_ctl stop -m fast”来关闭数据库,在PostgreSQL9.5以上的版本中可以直接用“pg_ctl stop”命令来关闭数据库。
启停演示
版本说明:
postgres@linuxpg51:5432=#select version();
version
-----------------------------------------------------------------------------------------------------
PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit
启动:
[pgsql@linuxpg51:/home/pgsql]$pg_ctl start -D /postgresql/pgdata/
waiting for server to start....2022-11-01 23:29:01.797 CST [33286] LOG: starting PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit
2022-11-01 23:29:01.797 CST [33286] LOG: listening on IPv4 address "0.0.0.0", port 5432
2022-11-01 23:29:01.797 CST [33286] LOG: listening on IPv6 address "::", port 5432
2022-11-01 23:29:01.798 CST [33286] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2022-11-01 23:29:02.735 CST [33286] LOG: redirecting log output to logging collector process
2022-11-01 23:29:02.735 CST [33286] HINT: Future log output will appear in directory "log".
done
server started
停止(fast):
pg_ctl stop -D /postgresql/pgdata/ -m fast
2022-11-01 23:28:13.689 CST [15716] LOG: received fast shutdown request
2022-11-01 23:28:13.691 CST [15716] LOG: aborting any active transactions
2022-11-01 23:28:13.692 CST [15716] LOG: background worker "logical replication launcher" (PID 15730) exited with exit code 1
2022-11-01 23:28:13.693 CST [15724] LOG: shutting down
2022-11-01 23:28:14.144 CST [15737] LOG: standby "walreceiver" is now a synchronous standby with priority 1
2022-11-01 23:28:14.145 CST [33237] FATAL: the database system is shutting down
2022-11-01 23:28:14.169 CST [15716] LOG: database system is shut down