PostgreSQL简单管理(一)

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介:

1、初始化数据库集群

和其他RDBMS一样,在开始使用PostgreSQL数据库之前需要在磁盘上初始化一个数据库,这里称为数据库集群。数据库集群是一个运行着的数据库服务实例管理的数据库的集合。初始化完后,集群中包含一个名为postgres的数据库,作为默认的数据库。还会创建另一个叫作template1的数据库,它被用作后续创建数据库的一个模版。

在文件系统层面,一个数据库集群是一个存储所有数据的目录(data directory)。它取决于你选择在哪存储你的数据。默认的目录是/usr/local/pgsql/data或/var/lib/pgsql/data。

使用iniddb命令初始化数据库集群,使用-D参数指定数据库的路径。示例如下:

initdb -D /usr/local/pgsql/data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[postgres@rhel7 ~]$ initdb -D /usr/ local /pgsql/data
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.
 
fixing permissions  on  existing directory /usr/ local /pgsql/data ... ok
creating subdirectories ... ok
selecting  default  max_connections ... 100
selecting  default  shared_buffers ... 128MB
selecting  dynamic  shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data  to  disk ... ok
 
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:
 
     pg_ctl -D /usr/ local /pgsql/data -l logfile start

可以指定环境变量PGDATA指向PostgreSQL的目录。

还可以调用pg_ctl命令来初始化数据库集群:

pg_ctl -D /usr/local/pgsql/data initdb

指定的目录必须为空,否则则无法初始化。初始化后整个目录的权限变为700(drwx------)。

1
2
3
[postgres@rhel7 pgsql]$ ls -l
total 4
drwx ------ 19 postgres postgres 4096 Mar 23 16:31 data

2、启动数据库服务

数据库服务程序叫作postgres。启动数据库时必须指定数据目录。简单的一个示例:

postgres -D /usr/local/pgsql/data

1
2
3
4
5
[postgres@rhel7 data]$ postgres -D /usr/ local /pgsql/data/
LOG:   database  system was shut down  at  2017-03-23 16:31:28 CST
LOG:  MultiXact member wraparound protections are now enabled
LOG:   database  system  is  ready  to  accept connections
LOG:  autovacuum launcher started

如果不指定-D参数,命令会去找PGDATA环境变量,如果两个都没有则启动报错。

上面的命令是在前台启动数据库服务,不过最好在后台启动。后台启动的例子:

postgres -D /usr/local/pgsql/data > logfile 2>&1 &

另一个封装好的命令pg_ctl也可以提供相应的功能。如下例:

pg_ctl start -l logfile 

1
2
3
4
5
6
7
[postgres@rhel7 data]$ pg_ctl -D /usr/ local /pgsql/data start -l logfile
server starting
[postgres@rhel7 data]$ cat logfile 
LOG:   database  system was shut down  at  2017-03-23 16:34:12 CST
LOG:  MultiXact member wraparound protections are now enabled
LOG:   database  system  is  ready  to  accept connections
LOG:  autovacuum launcher started

上面的命令可以在后台启用数据库服务,并把输出写入到日志文件中。-D参数同样用于指定数据目录。pg_ctl还可以用于停止数据库服务。

服务启动后,对应的PID被记录在数据目录的postmaster.pid文件中。防止启动多次,也可以用来关闭服务。

3、关闭数据库服务

关闭PostgreSQL数据库有多种模式。主要有如下几种:

SIGTERM

Smart Shutdown模式。数据库接到SIGTERM后,服务端不允许新的连接,但已连接的会话继续工作直到会话完成。当所有会话完成后才关闭数据库。如果数据库在热备状态,会等备份完成。如果在恢复状态则等所有进程终止。

SIGINT

Fast Shutdown模式。服务端不允许新的连接,并且给所有已存在的服务进程发送SIGTERM,使它们终止当前的事务并立即退出。所有服务进程退出后数据库关闭。

SIGQUIT

Immediate Shutdown模式。服务端发送SIGQUIT给所有的子进程,并等待它们终止,如果5秒钟后没有终止,这些进行被SIGKILL。等所有子进程停止后,主服务进程退出。它不做正常的关闭进程,下次启动时会启动恢复。建议仅在紧急情况下使用。

pg_ctl提供关闭数据库的方式:

pg_ctl stop #默认fast模式关闭

1
2
3
4
5
6
7
8
9
10
[postgres@rhel7 data]$ > logfile 
[postgres@rhel7 data]$ pg_ctl stop -D /usr/ local /pgsql/data/
waiting  for  server  to  shut down.... done
server stopped
[postgres@rhel7 data]$ cat logfile 
LOG:  received fast shutdown request
LOG:  aborting  any  active transactions
LOG:  autovacuum launcher shutting down
LOG:  shutting down
LOG:   database  system  is  shut down

指定用某种方式关闭:

pg_ctl stop -m smart/fast/immediate 

还可以直接kill进程号的方式,进程号在数据目录的postmaster.pid文件中

kill -INT ‘head -1 /usr/local/pgsql/data/postmaster.pid‘

4、服务端配置

参数配置文件在数据目录中的postgresql.conf

查看当前的配置

使用show命令

show all/paramter_name;

使用函数

select current_setting('paramter_name');

4.1、修改参数

4.1.1 使用SQL语句修改参数

ALTER SYSTEM   #修改系统级相当于修改psotgresql.conf,改后的参数记录在postgresql.auto.conf文件中

ALTER DATABASE #修改数据库级

ALTER ROLE     #修改ROLE级

直接修改postgresql.conf文件,需要pg_ctl reload或执行select pg_reload_conf();把配置重新读入系统;

另外,还有一个系统视图pg_settings可以用来查看及修改session级别的参数。

SET configuration_parameter TO DEFAULT;

UPDATE pg_settings SET setting = reset_val WHERE name = ’configuration_parameter’;

上面两个语句是等价的。

4.1.2 在命令行中指定参数

在启动数据库时,通过postgres命令使用-c添加指定参数

postgres -c log_connections=yes -c log_destination=’syslog’

这种方式指定的参数除非重启数据库,否则ALTER SYSTEM命令也不能修改。指定的参数信息记录在postmaster.opts文件中。

在启动session时,可以通过设置环境变量PGOPTIONS,来设置session中参数的值

env PGOPTIONS="-c geqo=off -c statement_timeout=5min" psql

4.1.3 引用其他参数文件

参数文件postgresql.conf可以引用其他参数文件,可以嵌套引用。可以由以下参数参数指定:

include='special.conf'  #直接指定文件,与postgresql.conf不在同一目录下需要指定绝对路径,如果文件不存在,则启动报错。

include_if_exists='exists.conf' #用法同include,但如果文件不存在则忽略该参数

include_dir='conf_dir'   #引用指定目录下所有的后缀为.conf的文件。


参考:https://www.postgresql.org/docs/9.6/static/server-start.html




      本文转自hbxztc 51CTO博客,原文链接:http://blog.51cto.com/hbxztc/1909893,如需转载请自行联系原作者






相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
3月前
|
存储 关系型数据库 Java
polardb有没有搞过pg 全量及增量备份管理的
【1月更文挑战第3天】【1月更文挑战第11篇】 polardb有没有搞过pg 全量及增量备份管理的
33 1
|
4月前
|
存储 Oracle 关系型数据库
postgresql数据库|wal日志的开启以及如何管理
postgresql数据库|wal日志的开启以及如何管理
289 0
|
5月前
|
消息中间件 存储 关系型数据库
PostgreSQL技术大讲堂 - 第33讲:并行查询管理
PostgreSQL从小白到专家,技术大讲堂 - 第33讲:并行查询管理
289 1
|
2月前
|
SQL 存储 缓存
PostgreSQL函数管理接口
学习PostgreSQL服务端开发必须要对函数管理接口有比较深入的了解
143 0
|
4月前
|
缓存 关系型数据库 MySQL
postgresql|数据库|序列Sequence的创建和管理
postgresql|数据库|序列Sequence的创建和管理
50 0
|
4月前
|
关系型数据库 Serverless 分布式数据库
PolarDB Serverless能力测评:秒级弹升、无感伸缩与强一致性,助您实现高效云数据库管理!
云原生数据库 PolarDB MySQL 版是阿里云自研产品,100%兼容 MySQL。PolarDB产品具有多主多写、多活容灾、HTAP 等特性,交易性能最高可达开源数据库的6倍,分析性能最高可达开源数据库的400倍,TCO 低于自建数据库50%。【评测用!】
70462 15
|
4月前
|
关系型数据库 Serverless 分布式数据库
PolarDB Serverless能力测评:秒级弹升、无感伸缩与强一致性,助您实现高效云数据库管理!
云原生数据库 PolarDB MySQL 版是阿里云自研产品,100%兼容 MySQL。PolarDB产品具有多主多写、多活容灾、HTAP 等特性,交易性能最高可达开源数据库的6倍,分析性能最高可达开源数据库的400倍,TCO 低于自建数据库50%。
|
9月前
|
缓存 关系型数据库 数据库
PostgreSQL技术大讲堂 - 第22讲:CLOG作用与管理
从零开始学PostgreSQL技术大讲堂 - 第22讲:CLOG作用与管理
208 1
|
11月前
|
存储 关系型数据库 定位技术
|
关系型数据库 数据库 文件存储
PG技术大讲堂 - 第12讲:PostgreSQL wal作用与管理
PostgreSQL从小白到专家,技术大讲堂 - 第12讲:PostgreSQL wal作用与管理
212 1