PostgreSQL数据库重启详解 (postgres数据库重启动)

在数据库应用过程中,由于各种原因,我们可能需要重启数据库,而PostgreSQL数据库作为一种开源的关系型数据库管理系统,数据库重启不仅是经常出现的情况,也是非常重要的操作,因为不正确的重启操作可能会导致数据丢失或系统崩溃。因此,本篇文章将详细介绍如何正确重启PostgreSQL数据库。

1. 了解常见的数据库重启方式

常见的数据库重启方式包括软件重启、系统重启和硬件重启。

1.1 软件重启

在软件重启的情况下,我们需要使用系统管理员或superuser的权限,让PostgreSQL进程停止并重新启动。在这种情况下,数据不会丢失,只是数据库的连接会被重置。

1.2 系统重启

系统重启是最常用的重启数据库方法之一。在这种情况下,操作系统会关闭PostgreSQL进程,并在系统启动时重新启动它。这种操作会直接关闭数据库,因此需要事先备份数据,并确保所有数据都已经保存。

1.3 硬件重启

硬件重启是非常危险的,因为它可能会导致数据丢失。如果是在关闭硬件的情况下进行重启,那么你需要备份所有数据,并确保所有数据都已经保存。

2. 在进行数据库重启前的准备工作

在进行数据库重启操作之前,我们需要做好以下准备工作:

2.1 确定是否需要重启

在进行数据库重启之前,我们需要确定是否需要进行重启操作。如果有任何错误报告或异常情况,我们需要首先检查日志文件以确保是否能够通过解决问题来避免重启操作。

2.2 备份数据

在数据库重启之前,我们需要备份数据。数据库的备份可以采用多种方式,例如使用pg_dump实用程序备份数据库。

2.3 关闭所有会话

在执行重启之前,我们需要关闭所有连接到数据库的会话,这可以通过授予系统管理员或superuser权限来实现。如果我们不关闭所有连接,则可以发生数据丢失或坏块的情况。

2.4 停止所有工作线程

在重启数据库之前,需要停止所有的工作线程。在PostgreSQL中,工作线程是负责执行用户查询和计算的进程。在关闭工作线程之前,需要先授予superuser或系统管理员权限。

2.5 关闭PostgreSQL实例

在执行操作之前,需要关闭正在运行的PostgreSQL实例。在关闭数据库之前,请确保没有其他程序在使用数据库服务。

3. PostgreSQL数据库重启操作步骤

在做好准备工作之后,我们可以按照以下步骤重启PostgreSQL数据库:

3.1 关闭数据库

在执行重启操作之前,我们需要先关闭当前运行的PostgreSQL实例。我们可以通过使用pg_ctl命令关闭PostgreSQL,命令如下:

$ sudo systemctl stop postgresql-13

3.2 启动PostgreSQL

在关闭PostgreSQL之后,可以通过以下命令重新启动数据库实例:

$ sudo systemctl start postgresql-13

3.3 检查数据库状态

启动数据库之后,我们需要检查数据库状态并确认所有进程已经完成。我们可以使用以下命令检查数据库状态:

$ sudo systemctl status postgresql-13

如果数据库状态显示为“活动(运行中)”,则表示数据库已经成功重启。

4.

在使用PostgreSQL数据库时,重启操作是非常常见的操作之一。然而,由于重启操作可能会影响到数据库的稳定性和数据完整性,因此需要进行严谨的准备和保护。在执行重启操作之前,需要备份所有数据,并关闭所有会话和工作线程。通过正确的操作步骤,我们可以确保PostgreSQL数据库能够安全高效地进行重启操作。

相关问题拓展阅读:

如何退出postgresq数据库

\q 退出postgresql数据库,建议使用\?查看帮助信息!

   启动数据库服务器(posgres用户)

  

$ postgres D /opt/postgresql/data/ > /opt/postgresql/log/pg_serverlog >& &

  当然如果设置了环境变量

  

PGDATA=/opt/postgresql/data

export PGDATA

  后可使用pg_ctl工具进行启动:

  

$ pg_ctl start l /opt/postgresql/log/pg_serverlog

pg_ctl: another server might be running; trying to start server anyway

pg_ctl: could not start server

Examine the log output

$

  因为之前已经启动所以打印another server might be running此时查看日志有如下信息:

  

$ cat pg_serverlog

FATAL: lock file postmasterpid already exists

HINT: Is another postmaster (PID ) running in data directory /opt/postgresql/data?

$

  当然最简的启动方式是

  

$ pg_ctl start

server starting

$ LOG: database system was shut down at :: CST

LOG: autovacuum launcher started

LOG: database system is ready to accept connections

  如果要在操作系统启动时就启动PG可以在/etc/信掘rcd/rclocal 文件中加以下语句

  

/opt/postgresql/bin/pg_ctl start l /opt/postgresql/log/pg_serverlog D /opt/postgresql/data

  关闭服务器

  最简单方法

  

$ pg_ctl stop

waiting for server to shut down done

server stopped

  与Oracle相同在关闭时也可采用不同的模式简介如下

  

SIGTERM

不再允许新的连接但是允许所有活跃的会话正常完成他们的工作只有在所有会话都结束任务后才关闭这是智能关闭

SIGINT

不再允许新的连接向所有活跃服务器发送 SIGTERM(让它们立刻退出)然后等待所有子进程退出并关闭数据库这是快速关闭

SIGQUIT

令 postgres 向所有子进程发送 SIGQUIT 并且立即退出(所有子进程也会立即退出)而不会妥善地关闭数据库系统这是立即关闭这样做会导致下次启动时的恢复(通过重放 WAL 日志)我们推荐只在紧急的时候使用这个方法

SIGKILL

此选项尽量不要使用这样会阻桥差止服务器清理共享内存和信号灯资源那样的话你只能在启动服务器之前自己手工做这件事另外SIGKILL 直接把 postgres 杀掉而不会等它把信号中继给它的敏坦皮子进程因此我们还需要手工杀掉每个独立子进程

  使用方法举例

  

$ pg_ctl stop o SIGTERM

LOG: received art shutdown request

LOG: autovacuum launcher shutting down

waiting for server to shut downLOG: shutting down

LOG: database system is shut down

done

server stopped

$

  最快速关闭方法kill postgres 进程

  

$ kill INT `head /opt/postgresql/data/postmasterpid`

$ LOG: received fast shutdown request

LOG: aborting any active transactions

LOG: autovacuum launcher shutting down

LOG: shutting down

LOG: database system is shut down

  附postgre启动后的进程如下:

  

$ ps ef|grep post

root: pts/ :: su postgres

postgres: pts/ :: bash

postgres: pts/ :: /opt/postgresql/bin/postgres

postgres: ?:: postgres: writer process

postgres: ?:: postgres: wal writer process

postgres: ?:: postgres: autovacuum launcher process

postgres: ?:: postgres: stats collector process

postgres: pts/ :: ps ef

postgres: pts/ :: grep post

$

直腔哪兆接在命令缓厅行输入:quit或伍租者是exist即可完成退出。

关于postgres数据库重启动的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。


数据运维技术 » PostgreSQL数据库重启详解 (postgres数据库重启动)