CentOS7 安装 PostgreSQL
一、CentOS7 编译安装 PostgreSQL-12.22PostgreSQL-12.22 及 PostgreSQL-14.15 版本的安装流程均遵照下文所述步骤
1. 编译安装
su root
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 的依赖包
wget https://ftp.postgresql.org/pub/source/v12.22/postgresql-12.22.tar.gz
tar -zxvf postgresql-12.22.tar.gz
cd postgresql-12.22
./configure --prefix=/usr/local/postgresql-12.22 # 指明安装路径为 `/usr/local/postgresql-12.22`
make -j$(nproc) && make install # 使用 $(nproc) 获取系统的CPU核心数,实现 make 任务的加速执行
2. 创建 postgres 用户
su root
groupadd postgres
useradd postgres -g postgres
passwd postgres
id postgres
cd /usr/local/postgresql-12.22
mkdir data log
chown -R postgres:postgres /usr/local/postgresql-12.22
chmod -R 700 /usr/local/postgresql-12.22
3. 利用 postgres 用户管理 PostgreSQL
su postgres
# 初始化
/usr/local/postgresql-12.22/bin/initdb /usr/local/postgresql-12.22/data
# 修改配置文件
vim /usr/local/postgresql-12.22/data/postgresql.conf
listen_addresses = '*'
port = 5432
vim /usr/local/postgresql-12.22/data/pg_hba.conf
host all all 0.0.0.0/0 md5 # 设置使用任意ip登录数据库需要使用基于 md5 加密策略验证用户身份
# 管理服务:开启、重启、重新加载配置文件、查看状态、停止
/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 restart
/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
/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
# 使用 psql 客户端登录数据库
/usr/local/postgresql-12.22/bin/psql -h 127.0.0.1 -p 5432 -d postgres -U postgres
/usr/local/postgresql-12.22/bin/psql -h 12.12.12.12 -p 5432 -d postgres -U postgres
# 修改数据库中 postgres 用户的密码
select usename, passwd from pg_shadow;
alter user postgres with password 'postgres123456';
4. 配置 systemctl 工具管理服务
在配置 systemctl 文件时,由于 systemctl 命令仅限 root 用户利用,而 PostgreSQL 的二进制文件要求以 postgres 用户身份执行,因此必须明确指定服务以 postgres 用户权限运行
su root
vim /etc/systemd/system/postgresql-12.22.service
Description=PostgreSQL 12.22
After=network.target
Type=forking
User=postgres
Group=postgres
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
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start postgresql-12.22.service
systemctl restart postgresql-12.22.service
systemctl status postgresql-12.22.service
systemctl enable postgresql-12.22.service
systemctl stop postgresql-12.22.service
reboot
5. 远程连接测试
至此编译安装 PostgreSQL 完成
https://i-blog.csdnimg.cn/direct/43dd021c85c740158fea20a13f776ac9.png#pic_center
6. 直接将安装目次复制到另一台机器
将安装目次打包
su root
tar -zcvf postgresql-12.22.tar.gz postgresql-12.22 将安装目次的压缩包上传到另一台服务器
su root
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 su postgres
/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 开机自启
su root
vim /etc/systemd/system/postgresql-12.22.service
Description=PostgreSQL 12.22
After=network.target
Type=forking
User=postgres
Group=postgres
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
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start postgresql-12.22.service
systemctl restart postgresql-12.22.service
systemctl status postgresql-12.22.service
systemctl enable postgresql-12.22.service
systemctl stop postgresql-12.22.service
reboot
二、CentOS7 利用 yum 工具安装 PostgreSQL-14
官网
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install -y postgresql14-server
/usr/pgsql-14/bin/postgresql-14-setup initdb
systemctl enable postgresql-14
systemctl start postgresql-14
systemctl status postgresql-14
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
rpm -ivh libicu-50.2-4.el7_7.x86_64.rpm
rpm -ivh libxslt-1.1.28-6.el7.x86_64.rpm
rpm -ivh postgresql14-libs-14.15-1PGDG.rhel7.x86_64.rpm
rpm -ivh postgresql14-14.15-1PGDG.rhel7.x86_64.rpm
rpm -ivh postgresql14-server-14.15-1PGDG.rhel7.x86_64.rpm
rpm -ivh postgresql14-contrib-14.15-1PGDG.rhel7.x86_64.rpm
/usr/pgsql-14/bin/postgresql-14-setup initdb
systemctl enable postgresql-14
systemctl start postgresql-14
systemctl status postgresql-14
systemctl stop postgresql-14
sql14-contrib-14.15-1PGDG.rhel7.x86_64.rpm
```sh/usr/pgsql-14/bin/postgresql-14-setup initdb
systemctl enable postgresql-14
systemctl start postgresql-14
systemctl status postgresql-14
systemctl stop postgresql-14
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]