暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

pg启动关闭命令总结

原创 唯唯 2024-11-06
159
pg_ctl 详解
[postgres@dldbxkip4563 ~]$pg_ctl --help
pg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server.

Usage:
  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]
  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

Common options:
  -D, --pgdata=DATADIR   location of the database storage area
  -s, --silent           only print errors, no informational messages
  -t, --timeout=SECS     seconds to wait when using -w option
  -V, --version          output version information, then exit
  -w, --wait             wait until operation completes (default)
  -W, --no-wait          do not wait until operation completes
  -?, --help             show this help, then exit
If the -D option is omitted, the environment variable PGDATA is used.

Options for start or restart:
  -c, --core-files       allow postgres to produce core files
  -l, --log=FILENAME     write (or append) server log to FILENAME
  -o, --options=OPTIONS  command line options to pass to postgres
                         (PostgreSQL server executable) or initdb
  -p PATH-TO-POSTGRES    normally not necessary

Options for stop or restart:
  -m, --mode=MODE        MODE can be "smart", "fast", or "immediate"

Shutdown modes are:
  smart       quit after all clients have disconnected
  fast        quit directly, with proper shutdown (default)
  immediate   quit without complete shutdown; will lead to recovery on restart

Allowed signal names for kill:
  ABRT HUP INT KILL QUIT TERM USR1 USR2

Report bugs to <pgsql-bugs@lists.postgresql.org>.
[postgres@dldbxkip4563 ~]$
日常案例
#启动数据库
pg_ctl -D /data/pgsqldata/5432/data -l logfile start
#停止数据库
pg_ctl stop -mf
脚本启动

使用脚本,使用root用户,需要修改linux脚本里面的路径等参数

/opt/postgresql-12.10/contrib/start-scripts/linux 

开机自启设置

找到启动脚本
/opt/postgresql-12.10/contrib/start-scripts/linux

复制文件到init.d目录下
cp linux /etc/init.d/postgresqld
修改postgresqld文件
#! /bin/sh

# chkconfig: 2345 98 02
# description: PostgreSQL RDBMS

# This is an example of a start/stop script for SysV-style init, such
# as is used on Linux systems.  You should edit some of the variables
# and maybe the 'echo' commands.
#
# Place this file at /etc/init.d/postgresql (or
# /etc/rc.d/init.d/postgresql) and make symlinks to
#   /etc/rc.d/rc0.d/K02postgresql
#   /etc/rc.d/rc1.d/K02postgresql
#   /etc/rc.d/rc2.d/K02postgresql
#   /etc/rc.d/rc3.d/S98postgresql
#   /etc/rc.d/rc4.d/S98postgresql
#   /etc/rc.d/rc5.d/S98postgresql
# Or, if you have chkconfig, simply:
# chkconfig --add postgresql
#
# Proper init scripts on Linux systems normally require setting lock
# and pid files under /var/run as well as reacting to network
# settings, so you should treat this with care.

# Original author:  Ryan Kirkpatrick <pgsql@rkirkpat.net>

# contrib/start-scripts/linux

## EDIT FROM HERE

# Installation prefix
prefix=/usr/local/pgsql

# Data directory
PGDATA="/data/pgsqldata/5432/data"

# Who to run the postmaster as, usually "postgres".  (NOT "root")
PGUSER=postgres

# Where to keep a log file
PGLOG="$PGDATA/serverlog"

# It's often a good idea to protect the postmaster from being killed by the
# OOM killer (which will tend to preferentially kill the postmaster because
# of the way it accounts for shared memory).  To do that, uncomment these
# three lines:
#PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
#PG_MASTER_OOM_SCORE_ADJ=-1000
#PG_CHILD_OOM_SCORE_ADJ=0
# Older Linux kernels may not have /proc/self/oom_score_adj, but instead
# /proc/self/oom_adj, which works similarly except for having a different
# range of scores.  For such a system, uncomment these three lines instead:
#PG_OOM_ADJUST_FILE=/proc/self/oom_adj
#PG_MASTER_OOM_SCORE_ADJ=-17
#PG_CHILD_OOM_SCORE_ADJ=0

## STOP EDITING HERE

# The path that is to be used for the script
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# What to use to start up the postmaster.  (If you want the script to wait
# until the server has started, you could use "pg_ctl start" here.)
DAEMON="$prefix/bin/postmaster"

# What to use to shut down the postmaster
PGCTL="$prefix/bin/pg_ctl"

prefix=/usr/local/pgsql --pgsql主目录
PGDATA="/data/pgsqldata/5432/data" --pg data目录
PGUSER=postgres --pg用户
PGLOG="$PGDATA/serverlog" --pg日志文件

修改文件的可执行权限:
chmod 755 /etc/init.d/postgresqld
使用命令启动数据库
[root@dldbxkip4563 /etc/init.d]#systemctl start postgresqld
[root@dldbxkip4563 /etc/init.d]#systemctl status postgresqld
● postgresqld.service - SYSV: PostgreSQL RDBMS
   Loaded: loaded (/etc/rc.d/init.d/postgresqld; bad; vendor preset: disabled)
   Active: active (exited) since Fri 2024-11-01 11:05:18 CST; 5s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 23756 ExecStart=/etc/rc.d/init.d/postgresqld start (code=exited, status=0/SUCCESS)

Nov 01 11:05:17 dldbxkip4563 systemd[1]: Starting SYSV: PostgreSQL RDBMS...
Nov 01 11:05:17 dldbxkip4563 su[23757]: (to postgres) root on none
Nov 01 11:05:18 dldbxkip4563 postgresqld[23756]: Starting PostgreSQL: ok
Nov 01 11:05:18 dldbxkip4563 systemd[1]: Started SYSV: PostgreSQL RDBMS.
[root@dldbxkip4563 /etc/init.d]#ps -ef | grep postgre
root     23576 23541  0 10:57 pts/1    00:00:00 su - postgres
postgres 23585 23576  0 10:57 pts/1    00:00:00 -bash
postgres 23779     1  0 11:05 ?        00:00:00 /usr/local/pgsql/bin/postmaster -D /data/pgsqldata/5432/data
postgres 23789 23779  0 11:05 ?        00:00:00 postgres: checkpointer
postgres 23790 23779  0 11:05 ?        00:00:00 postgres: background writer
postgres 23791 23779  0 11:05 ?        00:00:00 postgres: walwriter
postgres 23792 23779  0 11:05 ?        00:00:00 postgres: autovacuum launcher
postgres 23793 23779  0 11:05 ?        00:00:00 postgres: stats collector
postgres 23794 23779  0 11:05 ?        00:00:00 postgres: logical replication launcher
root     23801 23499  0 11:05 pts/0    00:00:00 grep --color=auto postgre
[root@dldbxkip4563 /etc/init.d]#su - postgres
Last login: Fri Nov  1 11:05:17 CST 2024
[postgres@dldbxkip4563 ~]$psql -U postgres
psql (12.10)
Type "help" for help.

postgres=#
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论