从零开始学PostgreSQL (三) & Linux RPM安装PostgreSQL


前言

rpm离线安装本质就是通过yum进行安装,只不过是在网络条件的限制下、无法联通互联网环境。可以用此处方式来进行替换,从而达到安装postgresql数据库的目的。

安装步骤

1、下载相关的rpm依赖包

PostgreSQL 官网下载网址:https://www.postgresql.org/download/
image.png

选择你需要下载的环境
  • 第一个选择的是系统,选择您的操作系统系列:这里我们系统以Linux中的Centos为例
  • 第二个是选择你环境的发行版本:这里我们选择的是Red Hat/Rocky/AlmaLinux

image.png
选择后会跳转到详细页面,在PostgreSQL Yum Repository模块下继续选择详细信息。
image.png

选择你的系统环境
    1. 选择需要安装的Postgresql版本,选择15,默认就会安装15下面最新的版本。
    1. 选择你系统版本号
    1. 选择系统的架构(一般都是默认的,且Centos系统都是x86架构)
    1. 就是查找出来,对应你选择的系统版本安装Postgresql需要执行的命令

注意,我们这里是通过rpm离线安装。
划到页面底部,通过 Direct RPM download 进行下载相关rpm包。

image.png
点击 direct download 即可跳转到下载页面,选择好对应版本及系统架构。
image.png
进入rpm下载界面,这里有许多与postgresql相关的rpm依赖包。但是我们只需要下载以下四个rpm。

依赖库包: postgresql15-libs-15.5-1PGDG.rhel7
客户端: postgresql15-15.5-1PGDG.rhel7
模块&分布式: postgresql15-contrib-15.5-1PGDG.rhel7
服务端: postgresql15-server-15.5-1PGDG.rhel7

image.png

下载完成后,将这四个rpm依赖包上传至服务器。

2、安装PostgreSQL数据库

[root@VM-16-10-centos pgrpm]# ll
total 8596
-rw-r--r-- 1 root root 1647136 Jan  3 02:04 postgresql15-15.5-1PGDG.rhel7.x86_64.rpm
-rw-r--r-- 1 root root  728676 Jan  3 02:04 postgresql15-contrib-15.5-1PGDG.rhel7.x86_64.rpm
-rw-r--r-- 1 root root  291704 Jan  3 02:05 postgresql15-libs-15.5-1PGDG.rhel7.x86_64.rpm
-rw-r--r-- 1 root root 6123568 Jan  3 02:05 postgresql15-server-15.5-1PGDG.rhel7.x86_64.rpm
[root@VM-16-10-centos pgrpm]# rpm -ivh postgresql15-libs-15.5-1PGDG.rhel7.x86_64.rpm
warning: postgresql15-libs-15.5-1PGDG.rhel7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 73e3b907: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:postgresql15-libs-15.5-1PGDG.rhel################################# [100%]
[root@VM-16-10-centos pgrpm]# rpm -ivh postgresql15-15.5-1PGDG.rhel7.x86_64.rpm
warning: postgresql15-15.5-1PGDG.rhel7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 73e3b907: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:postgresql15-15.5-1PGDG.rhel7    ################################# [100%]
[root@VM-16-10-centos pgrpm]# rpm -ivh postgresql15-server-15.5-1PGDG.rhel7.x86_64.rpm
warning: postgresql15-server-15.5-1PGDG.rhel7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 73e3b907: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:postgresql15-server-15.5-1PGDG.rh################################# [100%]
[root@VM-16-10-centos pgrpm]# rpm -ivh postgresql15-contrib-15.5-1PGDG.rhel7.x86_64.rpm
warning: postgresql15-contrib-15.5-1PGDG.rhel7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 73e3b907: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:postgresql15-contrib-15.5-1PGDG.r################################# [100%]

至此,数据库已完成安装。

注意:

  • 在安装过程中,可能会因为系统缺少相关依赖而报错,导致安装失败。

目前常见的确实依赖包如下

  • libxslt.x86_64 0:1.1.28-6.el7…x86_64.rpm
  • libzstd-1.5.2-1.el7.x86_64.rpm
  • libicu-50.2-4.el7_7.x86_64.rpm(可能需要)

下载方式:

3、配置PostgreSQL数据库

初始化数据库
[root@VM-16-10-centos ~]# /usr/pgsql-15/bin/postgresql-15-setup initdb
Initializing database ... OK    # 初始化数据库成功。
设置开机启动并启动数据库
[root@VM-16-10-centos ~]# systemctl enable postgresql-15
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-15.service to /usr/lib/systemd/system/postgresql-15.service.
[root@VM-16-10-centos ~]# systemctl start postgresql-15
检查数据库是否启动
# 方式一
[root@VM-16-10-centos ~]# ps -ef | grep postgre
postgres  2651     1  0 21:22 ?        00:00:00 /usr/pgsql-15/bin/postmaster -D /var/lib/pgsql/15/data/
postgres  2653  2651  0 21:22 ?        00:00:00 postgres: logger 
postgres  2654  2651  0 21:22 ?        00:00:00 postgres: checkpointer 
postgres  2655  2651  0 21:22 ?        00:00:00 postgres: background writer 
postgres  2657  2651  0 21:22 ?        00:00:00 postgres: walwriter 
postgres  2658  2651  0 21:22 ?        00:00:00 postgres: autovacuum launcher 
postgres  2659  2651  0 21:22 ?        00:00:00 postgres: logical replication launcher 
root      2808  1974  0 21:23 pts/4    00:00:00 grep --color=auto postgre

# 方式二
[root@VM-16-10-centos ~]# systemctl status postgresql-15
● postgresql-15.service - PostgreSQL 15 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-15.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2024-01-15 21:23:00 CST; 1min 10s ago
     Docs: https://www.postgresql.org/docs/15/static/
  Process: 2645 ExecStartPre=/usr/pgsql-15/bin/postgresql-15-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 2651 (postmaster)
   CGroup: /system.slice/postgresql-15.service
           ├─2651 /usr/pgsql-15/bin/postmaster -D /var/lib/pgsql/15/data/
           ├─2653 postgres: logger 
           ├─2654 postgres: checkpointer 
           ├─2655 postgres: background writer 
           ├─2657 postgres: walwriter 
           ├─2658 postgres: autovacuum launcher 
           └─2659 postgres: logical replication launcher 

Jan 15 21:23:00 VM-16-10-centos systemd[1]: Starting PostgreSQL 15 database server...
Jan 15 21:23:00 VM-16-10-centos postmaster[2651]: 2024-01-15 21:23:00.102 CST [2651] LOG:  redirecting lo...cess
Jan 15 21:23:00 VM-16-10-centos postmaster[2651]: 2024-01-15 21:23:00.102 CST [2651] HINT:  Future log ou...og".
Jan 15 21:23:00 VM-16-10-centos systemd[1]: Started PostgreSQL 15 database server.
Hint: Some lines were ellipsized, use -l to show in full.
修改数据库密码
[root@VM-16-10-centos ~]# su postgres
bash-4.2$ psql
could not change directory to "/root": Permission denied
psql (15.5)
Type "help" for help.

# 修改数据库密码语法   ALTER USER postgres WITH PASSWORD '所需要设置的密码';
postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
ALTER ROLE
postgres=# \q
-bash-4.2$ exit
logout
[root@VM-16-10-centos ~]# 

配置文件说明:

  • postgresql.conf

该文件包含一些通用设置,比如内存分配、新建database的默认存储位置、Postgresql服务器的IP地址、日志位置以及许多其他位置。

  • pg_hba.conf

该文件用于控制Postgresql服务器的访问权限,具体包括:允许哪些用户连接到哪个数据库,允许哪些IP地址连接到本服务器,以及制定连接时使用的身份验证模式。

  • pg_ident.conf

如果该文件存在,则系统会基于文件内容将当前登陆的操作系统用户映射为一个Postgresql数据库内部的身份来登陆。有些人会把操作系统的root用户映射为Postgresql的postgres超级用户账号。

修改pg远程连接配置文件

pg_hba.conf

[root@VM-16-10-centos ~]# vim /var/lib/pgsql/15/data/pg_hba.conf
# 快捷键 shift + G   跳转到末尾
# 在IPv4 添加下面内容

# IPv4 local connections:
host    all             all             0.0.0.0/0               md5

image.png

修改主配置文件

配置远程连接的地址和端口号

[root@VM-16-10-centos ~]# vim /var/lib/pgsql/15/data/postgresql.conf

放开listen_addresses和port的注释并把listen_addresses后的localhost改成 *
wq保存退出
image.png

移动数据库至指定目录

该步骤非必须,看实际情况决定。

  1. 前提条件
# 创建新目录
[root@VM-16-10-centos ~]# mkdir -p /data/pgsql/
# 修改目录所属组和所有者
[root@VM-16-10-centos ~]# chown postgres:postgres -R /data/pgsql
# 停止正在运行的数据库
[root@VM-16-10-centos ~]# systemctl stop postgresql-15
  1. 移动目录
# 移动目录下数据
[root@VM-16-10-centos ~]# mv /var/lib/pgsql/15/* /data/pgsql/
# 修改目录所属组和所有者
[root@VM-16-10-centos ~]# chown -R postgres:postgres /data/pgsql/
# 给目录授权
[root@VM-16-10-centos ~]# chmod 700 /data/pgsql/data -R
  1. 修改配置文件
# 修改指定的数据目录
root@VM-16-10-centos ~]# vi /usr/lib/systemd/system/postgresql-15.service
#修改Environment=PGDATA=/var/lib/pgsql/15/data/为
  Environment=PGDATA=/data/pgsql/data/

# 修改数据目录
[root@VM-16-10-centos ~]# vi /data/pgsql/data/postgresql.conf
#修改data_directory:
 data_directory = '/data/pgsql/data'
  1. 重新加载配置文件、重启数据库
[root@VM-16-10-centos ~]# systemctl daemon-reload
[root@VM-16-10-centos ~]# systemctl restart postgresql-15
[root@VM-16-10-centos ~]# systemctl status postgresql-15
● postgresql-15.service - PostgreSQL 15 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-15.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2024-01-15 21:39:14 CST; 5s ago
     Docs: https://www.postgresql.org/docs/15/static/
  Process: 6799 ExecStartPre=/usr/pgsql-15/bin/postgresql-15-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 6805 (postmaster)
   CGroup: /system.slice/postgresql-15.service
           ├─6805 /usr/pgsql-15/bin/postmaster -D /data/pgsql/data/
           ├─6807 postgres: logger 
           ├─6808 postgres: checkpointer 
           ├─6809 postgres: background writer 
           ├─6811 postgres: walwriter 
           ├─6812 postgres: autovacuum launcher 
           └─6813 postgres: logical replication launcher 

Jan 15 21:39:14 VM-16-10-centos systemd[1]: Starting PostgreSQL 15 database server...
Jan 15 21:39:14 VM-16-10-centos postmaster[6805]: 2024-01-15 21:39:14.697 CST [6805] LOG:  redirecting lo...cess
Jan 15 21:39:14 VM-16-10-centos postmaster[6805]: 2024-01-15 21:39:14.697 CST [6805] HINT:  Future log ou...og".
Jan 15 21:39:14 VM-16-10-centos systemd[1]: Started PostgreSQL 15 database server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@VM-16-10-centos ~]# 
  • 58
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值