m6米乐安卓版下载-米乐app官网下载
暂无图片
3
暂无图片
暂无图片
暂无图片

【pg15】 在 rocky 9 上安装 postgresql 15 的五种方法(总有一个你没用过🤪) -m6米乐安卓版下载

原创 严少安 2023-11-22
523

本文将基于 rocky linux 9.3 演示如何安装 postgresql 15。

共介绍五种安装方法:

  1. 通过 dnf install 安装 postgresql 数据库
  2. 通过 dnf module install 安装 postgresql 数据库
  3. 通过 podman 拉取 postgresql 镜像,并启动 postgresql 数据库
  4. 编译 postgresql 源码,并安装、启动数据库
  5. 使用 postgresql 系统角色配置安装 postgresql 数据库

环境信息

本文使用的操作系统为 rocky linux 9.3

[postgres@rocky9 ~]$ cat /etc/redhat-release rocky linux release 9.3 (blue onyx)

关于该系统的更多信息,请参阅:

一、通过 dnf install 安装 postgresql 数据库

依照惯例,对于新系统先装一遍数据库,保持原有的习惯,使用 dnf install 或者 yum install 进行安装。

在 rocky 9 中,默认情况下,会在 appstream 仓库里寻找安装包,我们看到这里选择了 postgresql server 13。

[shawnyan@rocky9 ~]$ sudo dnf install postgresql-server last metadata expiration check: 1:38:01 ago on wed 22 nov 2023 07:33:15 am utc. dependencies resolved. ========================================================================================================================= package architecture version repository size ========================================================================================================================= installing: postgresql-server x86_64 13.11-1.el9_2 appstream 5.7 m installing dependencies: postgresql x86_64 13.11-1.el9_2 appstream 1.5 m postgresql-private-libs x86_64 13.11-1.el9_2 appstream 134 k transaction summary ========================================================================================================================= install 3 packages total size: 7.3 m installed size: 29 m

我们可以自定义 yum 源,这里通过添加 pgdg 的 yum 源,来获取最新的安装包。

[pgdg15] name=postgresql 15 for rhel / rocky $releasever - $basearch baseurl=https://download.postgresql.org/pub/repos/yum/15/redhat/rhel-$releasever-$basearch enabled=1 gpgcheck=0

示例,安装 pg 15.5:

[shawnyan@rocky9 yum.repos.d]$ sudo dnf install postgresql15-server
last metadata expiration check: 0:00:32 ago on wed 22 nov 2023 09:15:27 am utc.
dependencies resolved.
=========================================================================================================================
 package                            architecture          version                            repository             size
=========================================================================================================================
installing:
 postgresql15-server                x86_64                15.5-1pgdg.rhel9                   pgdg15                6.1 m
installing dependencies:
 lz4                                x86_64                1.9.3-5.el9                        baseos                 58 k
 postgresql15                       x86_64                15.5-1pgdg.rhel9                   pgdg15                1.5 m
 postgresql15-libs                  x86_64                15.5-1pgdg.rhel9                   pgdg15                297 k
transaction summary
=========================================================================================================================
install  4 packages
total download size: 7.9 m
installed size: 35 m

这里补充一点, 2023-11-09, pgdg 发布了 pg15 系列的最新版本 postgresql 15.5,修复了 3 个安全漏洞和 55 个 bug,详情可参考

二、通过 dnf module install 安装 postgresql 数据库

从 yum 升级到 dnf 后,引入了新的模块化概念,我们可以通过配置 module 来覆盖 appstream 以获取软件包更新。

直接通过 dnf module install 安装 pg 15:

[shawnyan@rocky9 ~]$ sudo dnf module install postgresql:15/server last metadata expiration check: 2:01:54 ago on wed 22 nov 2023 07:33:15 am utc. dependencies resolved. ========================================================================================================================= package architecture version repository size ========================================================================================================================= installing group/module packages: postgresql-server x86_64 15.3-1.module el9.2.0 14925 a515b28a appstream 6.1 m installing dependencies: postgresql x86_64 15.3-1.module el9.2.0 14925 a515b28a appstream 1.6 m postgresql-private-libs x86_64 15.3-1.module el9.2.0 14925 a515b28a appstream 137 k installing module profiles: postgresql/server enabling module streams: postgresql 15 transaction summary ========================================================================================================================= install 3 packages total size: 7.8 m installed size: 32 m ... installed: postgresql-15.3-1.module el9.2.0 14925 a515b28a.x86_64 postgresql-private-libs-15.3-1.module el9.2.0 14925 a515b28a.x86_64 postgresql-server-15.3-1.module el9.2.0 14925 a515b28a.x86_64 complete!

或者,启用 postgresql 模块,然后直接用 dnf install 进行安装。

  • 查看 module
[shawnyan@rocky9 ~]$ dnf module list rocky linux 9 - appstream 2.7 mb/s | 7.4 mb 00:02 rocky linux 9 - baseos 2.1 mb/s | 2.2 mb 00:01 rocky linux 9 - extras 22 kb/s | 13 kb 00:00 rocky linux 9 - appstream name stream profiles summary maven 3.8 common [d] java project management and project comprehension tool nginx 1.22 common [d] nginx webserver nodejs 18 common [d], development, minimal, s2i javascript runtime nodejs 20 common [d], development, minimal, s2i javascript runtime php 8.1 common [d], devel, minimal php scripting language postgresql 15 [x] client, server [d] postgresql server and client module redis 7 common [d] redis persistent key-value database ruby 3.1 common [d] an interpreter of object-oriented scripting language hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled [shawnyan@rocky9 ~]$
  • 启用 module
[shawnyan@rocky9 ~]$ sudo dnf module enable postgresql last metadata expiration check: 1:59:13 ago on wed 22 nov 2023 07:33:15 am utc. dependencies resolved. ========================================================================================================================= package architecture version repository size ========================================================================================================================= enabling module streams: postgresql 15 transaction summary ========================================================================================================================= is this ok [y/n]: y complete! [shawnyan@rocky9 ~]$
  • 调用 module 安装 pg 15
[shawnyan@rocky9 ~]$ sudo dnf install postgresql-server last metadata expiration check: 2:06:30 ago on wed 22 nov 2023 07:33:15 am utc. dependencies resolved. ========================================================================================================================= package architecture version repository size ========================================================================================================================= installing: postgresql-server x86_64 15.3-1.module el9.2.0 14925 a515b28a appstream 6.1 m installing dependencies: postgresql x86_64 15.3-1.module el9.2.0 14925 a515b28a appstream 1.6 m postgresql-private-libs x86_64 15.3-1.module el9.2.0 14925 a515b28a appstream 137 k transaction summary ========================================================================================================================= install 3 packages total download size: 7.8 m installed size: 32 m ...

到此,我们讲解了两种使用 dnf 进行安装 pg 数据库的办法,在实际使用过程中,我们可能还会在内网搭建一套自行维护的仓库,这需要更多的步骤,但主要操作流程和上面大同小异。

三、通过 podman 拉取 postgresql 镜像,并启动 postgresql 数据库

从 rhel 9 开始,引入了 podman,并将其作为默认值。 podman 更加小巧、安全,而且不需要守护进程。

为了保留习惯,向下兼容,我们除了安装 podman 之外,还将安装 podman-docker

[shawnyan@rocky9 ~]$ sudo dnf install podman podman-docker last metadata expiration check: 2:12:02 ago on wed 22 nov 2023 07:33:15 am utc. dependencies resolved. ========================================================================================================================= package architecture version repository size ========================================================================================================================= installing: podman x86_64 2:4.6.1-5.el9 appstream 15 m podman-docker noarch 2:4.6.1-5.el9 appstream 66 k installing dependencies: conmon x86_64 2:2.1.8-1.el9 appstream 51 k shadow-utils-subid x86_64 2:4.9-8.el9 baseos 86 k transaction summary ========================================================================================================================= install 4 packages total download size: 15 m installed size: 51 m ... installed: conmon-2:2.1.8-1.el9.x86_64 podman-2:4.6.1-5.el9.x86_64 podman-docker-2:4.6.1-5.el9.noarch shadow-utils-subid-2:4.9-8.el9.x86_64 complete!

拉取镜像步骤和之前 docker 相似,只是需要注意 podman 已经替代了 docker。

[shawnyan@rocky9 ~]$ podman pull postgres:15 ✔ docker.io/library/postgres:15 trying to pull docker.io/library/postgres:15... getting image source signatures copying blob 0887062b031b done copying blob 7424befe58d6 done copying blob 136c0aa94f90 done copying blob 99d820fe3716 done copying blob 834a7fce6be7 done copying blob 1f7ce2fa46ab done copying blob 343a6a92954a done copying blob 54fbafc0c84c done copying blob 0780e4ae473b done copying blob 0e0518459d99 done copying blob eddd58e4f8a1 done copying blob 97c5119aca7d done copying blob a97ae632ca42 done copying config 8cde386e2e done writing manifest to image destination 8cde386e2e85cce0eb684d0b27ddf6a8abbecd05d5af43bbbdf7be12c47c44e8 [shawnyan@rocky9 ~]$ podman images repository tag image id created size docker.io/library/postgres 15 8cde386e2e85 12 days ago 427 mb

拉起 pg 容器。

[shawnyan@rocky9 ~]$ podman run --name pg15 -e postgres_password=12356 -d postgres:15 fe960ddeb6512856ff9983c1ffa26b74006b2dc59ccef59fb9a068baf1d31bd6 [shawnyan@rocky9 ~]$ podman ps container id image command created status ports names fe960ddeb651 docker.io/library/postgres:15 postgres 3 seconds ago up 3 seconds pg15 [shawnyan@rocky9 ~]$

连接 postgresql 服务器,并查看版本。

[shawnyan@rocky9 ~]$ podman exec -it pg15 psql -u postgres
psql (15.5 (debian 15.5-1.pgdg120 1))
type "help" for help.
postgres=# select version();
                                                       version
---------------------------------------------------------------------------------------------------------------------
 postgresql 15.5 (debian 15.5-1.pgdg120 1) on x86_64-pc-linux-gnu, compiled by gcc (debian 12.2.0-14) 12.2.0, 64-bit
(1 row)

四、编译 postgresql 源码,并安装、启动数据库

在之前的文章中, 【pg15】 自定义 dockerfile 构建 postgresql 15 编译版 docker 镜像 ,介绍过如何编译 pg 15,具体过程不再赘述,这里强调一下不同。

  1. 前文是在 docker 中,基于 centos 7 构建,文本是直接在 rocky linux 9.3 上编译构建。
  2. 前文使用的是 postgresql 15.2 源码,本文使用的是最新的 15.5 源码。

主要编译步骤如下:

wget https://mirrors.neusoft.edu.cn/postgresql/source/v15.5/postgresql-15.5.tar.gz tar xzf postgresql-15.5.tar.gz cd postgresql-15.5 ./configure --prefix=/opt/postgresql --with-extra-version="-shawnyan" make world make install-world

输出:

configure: using compiler=gcc (gcc) 11.4.1 20230605 (red hat 11.4.1-2) configure: using cflags=-wall -wmissing-prototypes -wpointer-arith -wdeclaration-after-statement -werror=vla -wendif-labels -wmissing-format-attribute -wimplicit-fallthrough=3 -wcast-function-type -wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -wno-format-truncation -wno-stringop-truncation -o2 configure: using cppflags= -d_gnu_source configure: using ldflags= -wl,--as-needed configure: creating ./config.status config.status: creating gnumakefile config.status: creating src/makefile.global config.status: creating src/include/pg_config.h config.status: creating src/include/pg_config_ext.h config.status: creating src/interfaces/ecpg/include/ecpg_config.h config.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.s config.status: linking src/backend/port/posix_sema.c to src/backend/port/pg_sema.c config.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c config.status: linking src/include/port/linux.h to src/include/pg_config_os.h config.status: linking src/makefiles/makefile.linux to src/makefile.port ... ... /usr/bin/mkdir -p '/opt/postgresql/bin' /usr/bin/install -c vacuumlo '/opt/postgresql/bin' make[2]: leaving directory '/home/shawnyan/postgresql-15.5/contrib/vacuumlo' make[1]: leaving directory '/home/shawnyan/postgresql-15.5/contrib' [shawnyan@rocky9 postgresql-15.5]$

安装完成后,初始化数据库,并启动数据库:

[postgres@rocky9 ~]$ pg_ctl -d /data/pgsql/15/data -l logfile start waiting for server to start.... done server started

连接数据库,查看版本:

[postgres@rocky9 ~]$ psql psql (15.5-shawnyan) type "help" for help. postgres=# select version(); version ------------------------------------------------------------------------------------------------------------------- postgresql 15.5-shawnyan on x86_64-pc-linux-gnu, compiled by gcc (gcc) 11.4.1 20230605 (red hat 11.4.1-2), 64-bit (1 row) postgres=#

五、使用 postgresql 系统角色配置安装 postgresql 数据库

从红帽8开始,引入了 postgresql 系统角色概念,便于使用 ansible 批量部署。

确切地说,是从 rhel 7 增加了新包 rhel-system-roles [1],但 postgresql 系统角色是从 rhel 8 才加入。

使用该系统角色可以非常便利的对 postgresql 进行安装、配置、部署。在 rhel 9 上,支持调用该系统角色来维护 pg 13 和 15。

在 rocky 9.3 中,需要先安装 ansiblerhel-system-roles

[shawnyan@rocky9 ~]$ sudo dnf install ansible rhel-system-roles last metadata expiration check: 0:10:38 ago on wed 22 nov 2023 10:49:04 am utc. dependencies resolved. ========================================================================================================================= package architecture version repository size ========================================================================================================================= installing: ansible noarch 1:7.7.0-1.el9 epel-aliyun 34 m rhel-system-roles noarch 1.22.0-2.el9 appstream 1.6 m installing dependencies: ansible-core x86_64 1:2.14.9-1.el9 appstream 2.2 m python3-cffi x86_64 1.14.5-5.el9 baseos 241 k python3-cryptography x86_64 36.0.1-4.el9 baseos 1.2 m python3-ply noarch 3.11-14.el9.0.1 baseos 103 k python3-pycparser noarch 2.20-6.el9 baseos 124 k python3-resolvelib noarch 0.5.4-5.el9 appstream 29 k sshpass x86_64 1.09-4.el9 appstream 27 k transaction summary ========================================================================================================================= install 9 packages total download size: 40 m installed size: 393 m ... installed: ansible-1:7.7.0-1.el9.noarch ansible-core-1:2.14.9-1.el9.x86_64 python3-cffi-1.14.5-5.el9.x86_64 python3-cryptography-36.0.1-4.el9.x86_64 python3-ply-3.11-14.el9.0.1.noarch python3-pycparser-2.20-6.el9.noarch python3-resolvelib-0.5.4-5.el9.noarch rhel-system-roles-1.22.0-2.el9.noarch sshpass-1.09-4.el9.x86_64 complete!

然后,准备 playbook:

--- - name: run role with minimal parameters hosts: localhost vars: postgresql_version: "15" roles: - rhel-system-roles.postgresql

下一步,执行 playbook:

[root@rocky9 shawnyan]# ansible-playbook pb.yml
[warning]: provided hosts list is empty, only localhost is available. note that the implicit localhost does not match 'all'
play [run role with minimal parameters] ********************************************************************************************************
task [gathering facts] *************************************************************************************************************************
ok: [localhost]
task [rhel-system-roles.postgresql : set platform/version specific variables] ******************************************************************
included: /usr/share/ansible/roles/rhel-system-roles.postgresql/tasks/set_vars.yml for localhost
task [rhel-system-roles.postgresql : ensure ansible_facts used by role] ************************************************************************
ok: [localhost]
task [rhel-system-roles.postgresql : set platform/version specific variables] ******************************************************************
skipping: [localhost] => (item=redhat.yml)
skipping: [localhost] => (item=rocky.yml)
skipping: [localhost] => (item=rocky_9.yml)
skipping: [localhost] => (item=rocky_9.3.yml)
skipping: [localhost]
task [rhel-system-roles.postgresql : check postgresql.conf] ************************************************************************************
ok: [localhost]
task [rhel-system-roles.postgresql : check tls crt file] ***************************************************************************************
ok: [localhost]
task [rhel-system-roles.postgresql : check tls private key file] *******************************************************************************
ok: [localhost]
task [rhel-system-roles.postgresql : gather the package facts] *********************************************************************************
ok: [localhost]
task [rhel-system-roles.postgresql : check if requested version is supported in the system (rhel8)] ********************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : check if requested version is supported in system (rhel9)] ************************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : check requested and installed version of postgresql] ******************************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : ensure required packages are installed] *******************************************************************
ok: [localhost]
task [rhel-system-roles.postgresql : init db] **************************************************************************************************
changed: [localhost]
task [rhel-system-roles.postgresql : start postgresql server] **********************************************************************************
changed: [localhost]
task [rhel-system-roles.postgresql : enable and start existing instance of postgresql server] **************************************************
ok: [localhost]
task [rhel-system-roles.postgresql : set password for super user] ******************************************************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : enable logging in by password] ****************************************************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : run provided sql script] **********************************************************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : manage certificates] **************************************************************************************
included: /usr/share/ansible/roles/rhel-system-roles.postgresql/tasks/certificate.yml for localhost
task [generate certificates] *******************************************************************************************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : install certificate from the default path] ****************************************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : install certificate from the default path] ****************************************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : install certificate from custom path] *********************************************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : install certificate from custom path] *********************************************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : install certificate file] *********************************************************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : install postgresql server private key] ********************************************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : configure pg_hba.conf] ************************************************************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : create postgresql directory in /etc] **********************************************************************
ok: [localhost]
task [rhel-system-roles.postgresql : link generated conf file with server one] *****************************************************************
changed: [localhost]
task [rhel-system-roles.postgresql : generate postgresql system-roles-internal.conf] ***********************************************************
ok: [localhost]
task [rhel-system-roles.postgresql : generate postgresql system-roles.conf] ********************************************************************
skipping: [localhost]
task [rhel-system-roles.postgresql : link generated conf file with server one] *****************************************************************
skipping: [localhost]
running handler [rhel-system-roles.postgresql : restart postgresql] ****************************************************************************
changed: [localhost]
play recap *************************************************************************************************************************************
localhost                  : ok=16   changed=4    unreachable=0    failed=0    skipped=17   rescued=0    ignored=0

此时,查看 postgres 服务可以看到服务已经处于运行状态。

[root@rocky9 shawnyan]# systemctl status postgresql
● postgresql.service - postgresql database server
     loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: disabled)
     active: active (running) since tue 2023-11-21 23:49:57 utc; 13s ago
    process: 20928 execstartpre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/success)
   main pid: 20930 (postmaster)
      tasks: 7 (limit: 22756)
     memory: 44.0m
        cpu: 96ms
     cgroup: /system.slice/postgresql.service
             ├─20930 /usr/bin/postmaster -d /var/lib/pgsql/data
             ├─20931 "postgres: logger "
             ├─20932 "postgres: checkpointer "
             ├─20933 "postgres: background writer "
             ├─20935 "postgres: walwriter "
             ├─20936 "postgres: autovacuum launcher "
             └─20937 "postgres: logical replication launcher "
nov 21 23:49:57 rocky9.shawnyan.cn systemd[1]: starting postgresql database server...
nov 21 23:49:57 rocky9.shawnyan.cn postmaster[20930]: 2023-11-21 23:49:57.564 utc [20930] log:  redirecting log output to logging collector process
nov 21 23:49:57 rocky9.shawnyan.cn postmaster[20930]: 2023-11-21 23:49:57.564 utc [20930] hint:  future log output will appear in directory "log".
nov 21 23:49:57 rocky9.shawnyan.cn systemd[1]: started postgresql database server.

连接数据库,查看版本:

[postgres@rocky9 ~]$ psql psql (15.3) type "help" for help. postgres=# \l list of databases name | owner | encoding | collate | ctype | icu locale | locale provider | access privileges ----------- ---------- ---------- ------------- ------------- ------------ ----------------- ----------------------- postgres | postgres | utf8 | en_us.utf-8 | en_us.utf-8 | | libc | template0 | postgres | utf8 | en_us.utf-8 | en_us.utf-8 | | libc | =c/postgres | | | | | | | postgres=ctc/postgres template1 | postgres | utf8 | en_us.utf-8 | en_us.utf-8 | | libc | =c/postgres | | | | | | | postgres=ctc/postgres (3 rows) postgres=# select version(); version -------------------------------------------------------------------------------------------------------------- postgresql 15.3 on x86_64-redhat-linux-gnu, compiled by gcc (gcc) 11.3.1 20221121 (red hat 11.3.1-4), 64-bit (1 row) postgres=# \q [postgres@rocky9 ~]$

总结

到此,我们已经介绍了五种 postgresql 数据库的安装方式,对于不同环境、不同需求,可以选择不同的方式,希望读者开卷有益。


  1. https://access.redhat.com/errata/rhea-2017:2369 ↩︎

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【米乐app官网下载的版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论

网站地图