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"不存在,点这里解决

相关推荐
百***35338 小时前
PostgreSQL_安装部署
数据库·postgresql
清静诗意21 小时前
Ubuntu 下 PostgreSQL 安装与配置完整指南
linux·ubuntu·postgresql
资深web全栈开发21 小时前
PostgreSQL 实战指南(面向 MySQL 开发者)
数据库·mysql·postgresql
hello_fracong21 小时前
PostgreSQL (零-1) Windows安装PostgreSQL
数据库·windows·postgresql
羑悻的小杀马特2 天前
openGauss 应用开发测评(PostgreSQL 接入方式)
数据库·postgresql·opengauss
踏浪无痕2 天前
PostgreSQL实例进程:从启动到运行的完整故事
数据库·postgresql
yaso_zhang2 天前
jetson开机之前自启脚本sudo ifconfig 如何不需要输入密码
数据库·postgresql
l1t3 天前
DeepSeek辅助编写转换DuckDB json格式执行计划到PostgreSQL格式的Python程序
数据库·python·postgresql·json·执行计划
盒马coding4 天前
PostgreSQL18新功能COPY命令变得更加用户友好
数据库·postgresql
小毅&Nora4 天前
【后端】【面试】 ③ PostgreSQL高级面试题(含答案与实战案例)
postgresql·面试·职场和发展