CentOS7安装PostgreSQL15

本文假设服务器是全新的,未曾安装pgsql,并且使用root用户通过yum安装。

准备

  1. 检查是否已安装libzstd
shell 复制代码
# 查询是否存在libzstd
yum search libzstd

# 下载并安装(谁能告诉我怎么知道libzstd包在这个地址里的^_^)
wget https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/l/libzstd-1.5.5-1.el7.x86_64.rpm
rpm -ivh libzstd-1.5.5-1.el7.x86_64.rpm
rm -f libzstd-1.5.5-1.el7.x86_64.rpm

如果缺少libzstd,在执行yum install -y postgresql15-server时将提示如下错误:

  1. 检查内存和存储
shell 复制代码
# 查看内存
free -g
# 查看存储(留意挂载目录)
df -h

安装

下载地址:www.postgresql.org/download/

bash 复制代码
# 安装rpm仓库
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# 安装pgsql
yum install -y postgresql15-server

# 初始化数据库并设置自启动
/usr/pgsql-15/bin/postgresql-15-setup initdb
systemctl enable postgresql-15
systemctl start postgresql-15
# 查看运行状态
systemctl status postgresql-15

指定数据目录(可选)

yum方式安装时无法提前指定数据目录。默认情况下:

  • 安装目录在/usr/pgsql-*/
  • 数据文件和配置文件等在/var/lib/pgsql/

现在我打算将数据文件和配置文件等移动到/data/pgsql/

  1. 移动数据目录
shell 复制代码
# 停止pgsql
systemctl stop postgresql-15
# 创建/data/pgsql目录并授权(注意:此时并不存在/data/pgsql目录,新服务器甚至不存在/data目录)
mkdir -p /data/pgsql
chown -R postgres:postgres /data/pgsql

# 移动目录
mv /var/lib/pgsql/15/* /data/pgsql/
  1. 修改配置文件

a)修改postgresql-15.service文件,将Environment=PGDATA=/var/lib/pgsql/15/data/修改为Environment=PGDATA=/data/pgsql/data/

shell 复制代码
vi /usr/lib/systemd/system/postgresql-15.service

b)修改postgresql.conf,指定数据目录的值为/data/pgsql/data

shell 复制代码
vi /data/pgsql/data/postgresql.conf
  1. 使配置生效
shell 复制代码
# 重新加载配置文件,重启数据库
systemctl daemon-reload
systemctl restart postgresql-15

# 查看运行状况
ps -ef| grep postgres
systemctl status postgresql-15

修改密码

shell 复制代码
# 切换到postgres账号
su postgres

# 登录数据库
psql
# 修改密码
alter role postgres with password '123456';
# 登出数据库
\q

# 退出postgres账号
exit

授权远程连接

  1. 修改pg_hba.conf
shell 复制代码
vi /data/pgsql/data/pg_hba.conf
  1. 修改postgresql.conf
shell 复制代码
vi /data/pgsql/data/pg_hba.conf
  1. 重启数据库
shell 复制代码
# 重启后可以通过navicat等工具进行远程连接
systemctl restart postgresql-15

低版本的navicat可能提示字段"datlastsysoid"不存在,点这里解决

相关推荐
__风__15 小时前
PostgreSQL kv(jsonb)存储
数据库·postgresql
九皇叔叔1 天前
【7】PostgreSQL 事务
数据库·postgresql
ldj20203 天前
2025 Centos 安装PostgreSQL
linux·postgresql·centos
坤坤不爱吃鱼4 天前
【MySQL\Oracle\PostgreSQL】迁移到openGauss数据出现的问题解决方案
mysql·postgresql·oracle
行星0084 天前
PostgreSQL大表创建分区实战
数据库·postgresql
heart000_115 天前
拯救海量数据:PostgreSQL分区表性能优化实战手册(附压测对比)
数据库·postgresql·性能优化
忧愁的锅盖儿15 天前
PostgreSQL(二十七)索引内部结构
数据库·postgresql
静听山水16 天前
PostgreSQL/Hologres 外部数据包装器系统表 pg_foreign_data_wrapper 详解
数据库·postgresql
进击ing小白17 天前
项目中PostGreSql数据库的维护
数据库·postgresql·oracle
ZaaaaacK19 天前
Linux系统远程操作和程序编译
linux·运维·postgresql