CentOS7 安装 PostgreSQL

打印 上一主题 下一主题

主题 964|帖子 964|积分 2892

一、CentOS7 编译安装 PostgreSQL-12.22

   PostgreSQL-12.22 及 PostgreSQL-14.15 版本的安装流程均遵照下文所述步骤
  1. 编译安装

  1. su root
复制代码
  1. yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake # 安装 PostgreSQL 的依赖包
  2. wget https://ftp.postgresql.org/pub/source/v12.22/postgresql-12.22.tar.gz
  3. tar -zxvf postgresql-12.22.tar.gz
  4. cd postgresql-12.22
  5. ./configure --prefix=/usr/local/postgresql-12.22 # 指明安装路径为 `/usr/local/postgresql-12.22`
  6. make -j$(nproc) && make install # 使用 $(nproc) 获取系统的CPU核心数,实现 make 任务的加速执行
复制代码
2. 创建 postgres 用户

  1. su root
复制代码
  1. groupadd postgres
  2. useradd postgres -g postgres
  3. passwd postgres
  4. id postgres
  5. cd /usr/local/postgresql-12.22
  6. mkdir data log
  7. chown -R postgres:postgres /usr/local/postgresql-12.22
  8. chmod -R 700 /usr/local/postgresql-12.22
复制代码
3. 利用 postgres 用户管理 PostgreSQL

  1. su postgres
复制代码
  1. # 初始化
  2. /usr/local/postgresql-12.22/bin/initdb /usr/local/postgresql-12.22/data
  3. # 修改配置文件
  4. vim /usr/local/postgresql-12.22/data/postgresql.conf
  5. listen_addresses = '*'
  6. port = 5432
  7. vim /usr/local/postgresql-12.22/data/pg_hba.conf
  8. host    all             all             0.0.0.0/0               md5 # 设置使用任意ip登录数据库需要使用基于 md5 加密策略验证用户身份
  9. # 管理服务:开启、重启、重新加载配置文件、查看状态、停止
  10. /usr/local/postgresql-12.22/bin/pg_ctl -D /usr/local/postgresql-12.22/data -l /usr/local/postgresql-12.22/log/log.txt start
  11. /usr/local/postgresql-12.22/bin/pg_ctl -D /usr/local/postgresql-12.22/data -l /usr/local/postgresql-12.22/log/log.txt restart
  12. /usr/local/postgresql-12.22/bin/pg_ctl -D /usr/local/postgresql-12.22/data -l /usr/local/postgresql-12.22/log/log.txt reload
  13. /usr/local/postgresql-12.22/bin/pg_ctl -D /usr/local/postgresql-12.22/data -l /usr/local/postgresql-12.22/log/log.txt status
  14. /usr/local/postgresql-12.22/bin/pg_ctl -D /usr/local/postgresql-12.22/data -l /usr/local/postgresql-12.22/log/log.txt stop
  15. # 使用 psql 客户端登录数据库
  16. /usr/local/postgresql-12.22/bin/psql -h 127.0.0.1 -p 5432 -d postgres -U postgres
  17. /usr/local/postgresql-12.22/bin/psql -h 12.12.12.12 -p 5432 -d postgres -U postgres
  18. # 修改数据库中 postgres 用户的密码
  19. select usename, passwd from pg_shadow;
  20. alter user postgres with password 'postgres123456';
复制代码
4. 配置 systemctl 工具管理服务

   在配置 systemctl 文件时,由于 systemctl 命令仅限 root 用户利用,而 PostgreSQL 的二进制文件要求以 postgres 用户身份执行,因此必须明确指定服务以 postgres 用户权限运行
  1. su root
复制代码
  1. vim /etc/systemd/system/postgresql-12.22.service
复制代码
  1. [Unit]
  2. Description=PostgreSQL 12.22
  3. After=network.target
  4. [Service]
  5. Type=forking
  6. User=postgres
  7. Group=postgres
  8. ExecStart=/usr/local/postgresql-12.22/bin/pg_ctl -D /usr/local/postgresql-12.22/data -l /usr/local/postgresql-12.22/log/log.txt start
  9. [Install]
  10. WantedBy=multi-user.target
复制代码
  1. systemctl daemon-reload
  2. systemctl start postgresql-12.22.service
  3. systemctl restart postgresql-12.22.service
  4. systemctl status postgresql-12.22.service
  5. systemctl enable postgresql-12.22.service
  6. systemctl stop postgresql-12.22.service
  7. reboot
复制代码
5. 远程连接测试

   至此编译安装 PostgreSQL 完成
  

6. 直接将安装目次复制到另一台机器

   将安装目次打包
  1. su root
  2. tar -zcvf postgresql-12.22.tar.gz postgresql-12.22
复制代码
  将安装目次的压缩包上传到另一台服务器
  1. su root
  2. yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmakegroupadd postgresuseradd postgres -g postgrespasswd postgresid postgrestar -zxvf postgresql-12.22.tar.gzchmod -R 700 /usr/local/postgresql-12.22chown -R postgres:postgres /usr/local/postgresql-12.22
复制代码
  1. su postgres
  2. /usr/local/postgresql-12.22/bin/pg_ctl -D /usr/local/postgresql-12.22/data -l /usr/local/postgresql-12.22/log/log.txt start/usr/local/postgresql-12.22/bin/pg_ctl -D /usr/local/postgresql-12.22/data -l /usr/local/postgresql-12.22/log/log.txt status/usr/local/postgresql-12.22/bin/pg_ctl -D /usr/local/postgresql-12.22/data -l /usr/local/postgresql-12.22/log/log.txt stop
复制代码
  开机自启
  1. su root
  2. vim /etc/systemd/system/postgresql-12.22.service
复制代码
  1. [Unit]
  2. Description=PostgreSQL 12.22
  3. After=network.target
  4. [Service]
  5. Type=forking
  6. User=postgres
  7. Group=postgres
  8. ExecStart=/usr/local/postgresql-12.22/bin/pg_ctl -D /usr/local/postgresql-12.22/data -l /usr/local/postgresql-12.22/log/log.txt start
  9. [Install]
  10. WantedBy=multi-user.target
复制代码
  1. systemctl daemon-reload
  2. systemctl start postgresql-12.22.service
  3. systemctl restart postgresql-12.22.service
  4. systemctl status postgresql-12.22.service
  5. systemctl enable postgresql-12.22.service
  6. systemctl stop postgresql-12.22.service
  7. reboot
复制代码
二、CentOS7 利用 yum 工具安装 PostgreSQL-14

   官网
  1. yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
  2. yum install -y postgresql14-server
  3. /usr/pgsql-14/bin/postgresql-14-setup initdb
  4. systemctl enable postgresql-14
  5. systemctl start postgresql-14
  6. systemctl status postgresql-14
  7. systemctl stop postgresql-14
复制代码
  利用 yum 安装 PostgreSQL时
  

  • 自动创建 postgres 用户
  • 数据目次 data 位于 /var/lib/pgsql/14/data
  • 二进制可执行文件目次 bin 位于 /usr/pgsql-14/bin
  三、利用 rpm 离线安装 PostgreSQL-14

   进入官网,下载所必要的 rpm 包到服务器,必要保证各个软件包的小版本保持一致,同时利用 rpm 离线安装必要提前下载所需依赖包的 rpm 软件包
  安装时必要按照如下顺序【这里的软件包大概不全,可先执行命令安装,安装提示找缺少的 rpm 包安装即可】
  

  • libicu-50.2-4.el7_7.x86_64.rpm
  • libxslt-1.1.28-6.el7.x86_64.rpm
  • postgresql14-libs-14.15-1PGDG.rhel7.x86_64.rpm
  • postgresql14-14.15-1PGDG.rhel7.x86_64.rpm
  • postgresql14-server-14.15-1PGDG.rhel7.x86_64.rpm
  • postgresql14-contrib-14.15-1PGDG.rhel7.x86_64.rpm
  安装完成后
  

  • 自动创建 postgres 用户
  • 数据目次 data 位于 /var/lib/pgsql/14/data
  • 二进制可执行文件目次 bin 位于 /usr/pgsql-14/bin
  1. rpm -ivh libicu-50.2-4.el7_7.x86_64.rpm
  2. rpm -ivh libxslt-1.1.28-6.el7.x86_64.rpm
  3. rpm -ivh postgresql14-libs-14.15-1PGDG.rhel7.x86_64.rpm
  4. rpm -ivh postgresql14-14.15-1PGDG.rhel7.x86_64.rpm
  5. rpm -ivh postgresql14-server-14.15-1PGDG.rhel7.x86_64.rpm
  6. rpm -ivh postgresql14-contrib-14.15-1PGDG.rhel7.x86_64.rpm
复制代码
  1. /usr/pgsql-14/bin/postgresql-14-setup initdb
  2. systemctl enable postgresql-14
  3. systemctl start postgresql-14
  4. systemctl status postgresql-14
  5. systemctl stop postgresql-14
复制代码
sql14-contrib-14.15-1PGDG.rhel7.x86_64.rpm
  1. ```sh/usr/pgsql-14/bin/postgresql-14-setup initdb
  2. systemctl enable postgresql-14
  3. systemctl start postgresql-14
  4. systemctl status postgresql-14
  5. systemctl stop postgresql-14
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

天津储鑫盛钢材现货供应商

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表