安装 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
相关推荐
会编程的土豆4 分钟前
功能详解 01 · 用户注册:从表单到数据库一行
开发语言·数据库·后端·mysql·golang
夏贰四6 分钟前
数据资产平台如何落地企业数据分级合规管控?数据资产平台怎样满足行业数据监管审查要求?
大数据·数据库·人工智能
吴声子夜歌16 分钟前
MongoDB 4.2——聚合框架
数据库·mongodb
墨神谕32 分钟前
认识一下Unicode(统一码)
android·java·数据库
xywww1681 小时前
Claude Opus 5 API 接入实战:国内项目上线前的网络、Key、限流和排错清单
大数据·linux·网络·数据库·云计算·aws
yuezhilangniao1 小时前
某专科医院老数据库迁移实战记录-Oracle10g sqlserver2008等
数据库
白猫不黑1 小时前
SQL注入实战:手工注入全流程详解
网络·数据库·sql·web安全·网络安全·信息安全
kirs_ur11 小时前
ECC & LDPC — SSD 的数据卫士
服务器·数据库·性能优化
是三一seven11 小时前
Sql注入基础
数据库·安全·网络安全
Sirens.12 小时前
MySQL表设计进阶-约束范式连接索引与事务
android·数据库·mysql