安装 PostgreSQL

1.安装 PostgreSQL

bash 复制代码
sudo yum install -y postgresql postgresql-server
bash 复制代码
root@localhost ~]# systemctl status postgresql.service 
● postgresql.service - PostgreSQL database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2024-12-11 14:09:49 CST; 6s ago
  Process: 64216 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=1/FAILURE)

12月 11 14:09:49 localhost.localdomain systemd[1]: Starting PostgreSQL database server...
12月 11 14:09:49 localhost.localdomain postgresql-check-db-dir[64216]: Directory "/var/lib/pgsql/data" is missing or empty.
12月 11 14:09:49 localhost.localdomain postgresql-check-db-dir[64216]: Use "/usr/bin/postgresql-setup --initdb"
12月 11 14:09:49 localhost.localdomain postgresql-check-db-dir[64216]: to initialize the database cluster.
12月 11 14:09:49 localhost.localdomain postgresql-check-db-dir[64216]: See /usr/share/doc/postgresql/README.rpm-dist for more information.
12月 11 14:09:49 localhost.localdomain systemd[1]: postgresql.service: Control process exited, code=exited status=1
12月 11 14:09:49 localhost.localdomain systemd[1]: postgresql.service: Failed with result 'exit-code'.
12月 11 14:09:49 localhost.localdomain systemd[1]: Failed to start PostgreSQL database server.

出现这个错误是因为 PostgreSQL 的数据目录 /var/lib/pgsql/data 不存在或为空。要解决这个问题,需要初始化数据库集群。

按照以下步骤修复问题:

步骤 1:初始化 PostgreSQL 数据目录

执行初始化命令:

bash 复制代码
[root@localhost ~]# sudo /usr/bin/postgresql-setup --initdb
 * Initializing database in '/var/lib/pgsql/data'
 * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log

这将初始化 PostgreSQL 的数据目录。

步骤 2:启动 PostgreSQL 服务

启动服务:

bash 复制代码
sudo systemctl start postgresql.service

检查服务状态:

bash 复制代码
sudo systemctl status postgresql.service

设置服务开机自启动:

bash 复制代码
sudo systemctl enable postgresql.service

步骤 3 :创建数据库和用户

bash 复制代码
sudo -i -u postgres
psql

在 PostgreSQL shell 中运行以下命令:

bash 复制代码
CREATE DATABASE confluence;
CREATE USER confluence_user WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE confluence TO confluence_user;
ALTER ROLE confluence_user WITH LOGIN;
\q
exit

步骤 4:配置允许远程连接

编辑/var/lib/pgsql/data/pg_hba.conf,添加:

bash 复制代码
host    all             all             0.0.0.0/0            md5

编辑 /var/lib/pgsql/data/postgresql.conf,修改:

bash 复制代码
listen_addresses = '*'

重启服务:

bash 复制代码
sudo systemctl restart postgresql
相关推荐
羱滒8 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
程序员Android11 分钟前
MTK多帧拍照流程分析
数据库
不剪发的Tony老师12 分钟前
Apache Doris:一款高性能的实时数据仓库
数据库·数据仓库
甜可儿24 分钟前
redis序列化设置
数据库·redis
GGBondlctrl28 分钟前
【SpringBoot】论坛项目中如何进行实现发布文章,以及更新对应数据库的数据更新
数据库·springboot项目·三层设计思想·文章发布项目
若云止水42 分钟前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
jay丿1 小时前
Django模板系统深入
数据库·django·sqlite
千层冷面2 小时前
Redis除了做缓存还能做什么?
数据库·redis·缓存
码有余悸3 小时前
Redis:高性能的键值存储系统
数据库·redis·缓存
剑客狼心7 小时前
Oracle:什么是存储过程
数据库·oracle