检查PostgreSQL错误的要点
验证环境
在CentOS8和Postgres12.9上进行确认。
达到的目的
以下是在进行PostgreSQL验证过程中遇到的错误处理总结。可能有错误或误解的内容。
数据库集群(初始化)
◆错误
initdb: 错误: 目录”/var/lib/pgsql/data”已存在但不为空~
◆内容
如果有数据文件会出现错误。
◆对策
· 删除目录
· 更改现有目录名称
· 在创建目录时指定其他位置 等
启动、停止数据库
如果找不到数据库集群。
将规定值的目录更名并进行测试。
原本的值:/var/lib/pgsql/data
↓
更改后:/var/lib/pgsql/data1
进行测试。
以root用户身份尝试使用systemctl启动。
[root@localhost ~]# systemctl start postgresql.service
Job for postgresql.service failed because the control process exited with error code.
See "systemctl status postgresql.service" and "journalctl -xe" for details.
被告知要检查journalctl以获取错误信息。
[root@localhost ~]# journalctl -xe
-- Unit postgresql.service has begun starting up.
6月 05 10:46:39 localhost.localdomain postgresql-check-db-dir[188431]: Directory "/var/lib/pgsql/data" is missing or empty.
找不到/var/lib/pgsql/data。
尝试使用 pg_ctl 来启动。
[postgres@localhost ~]$ pg_ctl start
pg_ctl: ディレクトリ "/var/lib/pgsql/data" は存在しません
由于错误信息返回的是日语,因此很容易理解。
处理
如果找不到数据库群集,请考虑以下操作:
1.使用initdb创建数据库群集。
2.指定数据库群集并启动。
对于上述情况,可以使用”pg_ctl start -D /var/lib/pgsql/data1″等命令。
然而,停止时也需要指定目录。
无法访问数据库集群。
使用Postgres将数据库集群的权限设置为root并启动。
[root@localhost ~]# chown root:root /var/lib/pgsql/data
[root@localhost ~]# su - postgres
[postgres@localhost ~]$ pg_ctl start
pg_ctl: PIDファイル"/var/lib/pgsql/data/postmaster.pid"をオープンできませんでした: 許可がありません
由于无法从postgres用户访问,导致出现错误。
#### 处理方式
把所有者和群组设为postgres。
chown -R postgres:postgres /var/lib/pgsql。
将权限设为0750。
chmod -R 750 /var/lib/pgsql
无法与客户端连接。
情况1:postgresql.conf
请确认 listen_addresses 是否已经被允许。
★处理:允许连接来源
例
listen_addresses = ‘*’
※不能对listen_addresses进行范围设置,个别指定需在pg_hba.conf中进行。
案例2:pg_hba.conf
连接的数据库、用户、IP地址等是否被允许?
★设置示例
例:如果使用192.168.11.1进行密码(md5)认证
主机 全部 全部 192.168.11.1/32 md5
例:如果在192.168.11范围内进行密码(md5)认证,请使用子网掩码24。
host all all 192.168.121.0/24 md5
在指定复制时不需要密码的情况下,主机复制rep_user 192.168.121.123/32 信任。
SQL查询没有返回结果
在复制验证时遇到了困难。
如果synchronous_standby_names有效且synchronous_commit不为off且备用服务器停止导致无响应,则无法返回结果。
★处理方式:
– 启动待机
或者
– 将synchronous_commit设置为off或者本地以外的其他值
但是由于同步将不稳定,不建议使用。
无法停止数据库
如果在启动时将数据目录设置为环境变量之外的其他位置,则会出错。
通过stop命令指定数据目录。
当你遇到困难时需要研究的要点
1. 操作系统的日志记录
journalctl -xe
※ 用于显示由systemd-journald收集的日志的命令
2. PostgreSQL的日志
数据/日志
3. 进程确认
ps -ef | grep postgres
确认程序的位置(目录,用户命令的位置),可以执行以下命令:pg_config –bindir。
5. 查看环境变量
输出$PATH命令
6. 检查数据文件夹
连接到 psql
执行 SHOW data_directory;
[postgres@localhost ~]$ psql
psql (12.9)
"help"でヘルプを表示します。
postgres=# SHOW data_directory;
data_directory
---------------------
/var/lib/pgsql/data
(1 行)
7. 查询Postgres的版本
psql –version
在中国当地,通过systemctl可以在启动时检查状态,以确认程序的位置和数据目录。
[root@localhost ~]# systemctl status postgresql.service
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor >
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor >
Active: active (running) since Thu 2023-06-08 23:10:01 JST; 6min ago
Main PID: 1060 (postmaster)
Tasks: 8 (limit: 11095)
Memory: 24.5M
CGroup: /system.slice/postgresql.service
tq1060 /usr/bin/postmaster -D /var/lib/pgsql/data
tq1094 postgres: logger
tq1098 postgres: checkpointer
tq1099 postgres: background writer
tq1100 postgres: walwriter
tq1101 postgres: autovacuum launcher
tq1102 postgres: stats collector
mq1103 postgres: logical replication launcher
请参考下面的文章。