postgresql常用操作命令

1.查看编译参数
	postgres@postgresql-1:~$ pg_config
	返回结果:
	pg_config       pg_controldata  
	BINDIR = /usr/local/pgsql/bin
	DOCDIR = /usr/local/pgsql/share/doc
	HTMLDIR = /usr/local/pgsql/share/doc
	INCLUDEDIR = /usr/local/pgsql/include
	PKGINCLUDEDIR = /usr/local/pgsql/include
	INCLUDEDIR-SERVER = /usr/local/pgsql/include/server
	LIBDIR = /usr/local/pgsql/lib
	PKGLIBDIR = /usr/local/pgsql/lib
	LOCALEDIR = /usr/local/pgsql/share/locale
	MANDIR = /usr/local/pgsql/share/man
	SHAREDIR = /usr/local/pgsql/share
	SYSCONFDIR = /usr/local/pgsql/etc
	PGXS = /usr/local/pgsql/lib/pgxs/src/makefiles/pgxs.mk
	CONFIGURE =  '--prefix=/usr/local/pgsql'
	CC = gcc
	CPPFLAGS = -D_GNU_SOURCE
	CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2
	CFLAGS_SL = -fPIC
	LDFLAGS = -Wl,--as-needed -Wl,-rpath,'/usr/local/pgsql/lib',--enable-new-dtags
	LDFLAGS_EX = 
	LDFLAGS_SL = 
	LIBS = -lpgcommon -lpgport -lz -lreadline -lm 
	VERSION = PostgreSQL 14.2

2.pg_ctl 命令管理 PostgreSQL
	
	pg_ctl 是一个实用的命令行工具,有以下常见功能:
	
	初始化 PostgreSQL 数据库实例
	
	启动、终止或重启 PostgreSQL 数据库服务。
	
	查看 PostgreSQL数据库服务的状态
	
	让数据库实例重新读取配置文件。允许给一个指定的PostgreSQL进程发送信号
	
	控制 standby 服务器为可读写
	
	在 Windows平台下允许为数据库实例注册或取消一个系统服务
	
	查看pg_ctl命令帮助信息
		postgres@postgresql-1:~$ 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>.
		PostgreSQL home page: <https://www.postgresql.org/>
		
		解释:
			pg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server
				可以用pg_ctl启动停止等操作,例如pg_ctl start,pg_ctl stop
			
			pg_ctl init[db]
				利用pg_ctl initdb命令初始化数据库
				
			
3.初始化实例,多实例
	3.1先来给/pgsql/目录授权为postgres属主属组
		root@postgresql-1:~# chown -R postgres.postgres /pgsql/
	
	3.2切换用户
		root@postgresql-1:~# su - postgres
		
	3.3停止掉原有的pgsql数据库实例
		pg_ctl -D /pgsql/data stop
	
	3.4初始化多实例,数据库实例的数据存放路径放在/pgsql/data2下
	
		postgres@postgresql-1:~$ pg_ctl init -D /pgsql/data2
		返回结果:
		
		The files belonging to this database system will be owned by user "postgres".
		This user must also own the server process.
		
		The database cluster will be initialized with locale "en_US.UTF-8".
		The default database encoding has accordingly been set to "UTF8".
		The default text search configuration will be set to "english".
		
		Data page checksums are disabled.
		
		creating directory /pgsql/data2 ... ok
		creating subdirectories ... ok
		selecting dynamic shared memory implementation ... posix
		selecting default max_connections ... 100
		selecting default shared_buffers ... 128MB
		selecting default time zone ... Etc/UTC
		creating configuration files ... ok
		running bootstrap script ... ok
		performing post-bootstrap initialization ... ok
		syncing data to disk ... ok
		
		initdb: warning: enabling "trust" authentication for local connections
		You can change this by editing pg_hba.conf or using the option -A, or
		--auth-local and --auth-host, the next time you run initdb.
		
		Success. You can now start the database server using:
		
			/usr/local/pgsql/bin/pg_ctl -D /pgsql/data2 -l logfile start
		
	3.5修改新实例的端口为5433
		postgres@postgresql-1:~$ echo "port = 5433" >> /pgsql/data2/postgresql.conf
		
	3.6启动新实例和老实例
		3.6.1启动新实例
			postgres@postgresql-1:~$ /usr/local/pgsql/bin/pg_ctl -D /pgsql/data2 -l logfile start
			waiting for server to start.... done
			server started
		
		3.6.2启动老实例
			postgres@postgresql-1:~$ /usr/local/pgsql/bin/pg_ctl -D /pgsql/data -l logfile start
			waiting for server to start.... done
			server started
	
		3.6.3查看端口,如果看到5432和5433说明两个实例都启动成功了
			postgres@postgresql-1:~$ ss -ntl
			State            Recv-Q           Send-Q                     Local Address:Port                      Peer Address:Port           Process           
			LISTEN           0                244                            127.0.0.1:5432                           0.0.0.0:*                                
			LISTEN           0                244                            127.0.0.1:5433                           0.0.0.0:*                                
			LISTEN           0                128                              0.0.0.0:22                             0.0.0.0:*                                
			LISTEN           0                4096                       127.0.0.53%lo:53                             0.0.0.0:*                                
			LISTEN           0                128                                 [::]:22                                [::]:* 
			
		3.6.4查看两个实例的状态
			postgres@postgresql-1:~$ pg_ctl -D /pgsql/data status
			pg_ctl: server is running (PID: 1173)
			/usr/local/pgsql/bin/postgres "-D" "/pgsql/data"
			postgres@postgresql-1:~$ pg_ctl -D /pgsql/data2 status
			pg_ctl: server is running (PID: 1160)
			/usr/local/pgsql/bin/postgres "-D" "/pgsql/data2"
		
		


Powered By Z-BlogPHP 1.7.3

 Theme By 优美模版

本博客为开源博客,本博客联系QQ:372097243