Centos环境下离线安装postgreSQL数据库

使用 yum 在线安装时报错:为仓库 'pgdg-common' 下载元数据失败 : repomd.xml GPG signature verification error: Bad GPG signature

离线安装 postgreSql 方法:

下载数据库资源文件:PostgreSQL: File Browser

​
curl -O https://ftp.postgresql.org/pub/source/v17.5/postgresql-17.5.tar.gz

​

安装以及配置数据库

1.安装数据库

1.1. 新建目录,指定安装位置

mkdir –p /opt/postgresql

1.2. 将安装包解压到目录

tar –zxvf postgresql-17.5.tar.gz –C /opt/postgresql

1.3. 进入到解压目录内

cd /opt/postgresql/postgresql-17.5

1.4. 开始配置编译

./configure --prefix=/opt/postgresql/pgsql --without-readline
make
make install

1.5. 创建用户

cd /opt/postgresql/pgsql
adduser postgres

1.6. 创建data目录用于存储数据,创建log目录用于存储日志信息

mkdir {data,log}
chown postgres /opt/postgresql/pgsql/data
chown postgres /opt/postgresql/pgsql/log

1.7. 初始化数据库

su – postgres
./bin/initdb –D data/ -E UTF-8
./bin/postgres –D data/ >log/logfile 2>&1 &

2.测试

2.1. 创建数据库

./bin/createdb test

2.2. 进入pgsql shell

./bin/psql test

2.3. 退出命令

\q

3.修改密码

# ./bin/psql -h ip -U postgres(本地登录不指定IP不需要密码)

./bin/psql postgres
\password postgres
\q

4.修改数据库配置

vim data/postgresql.conf
vim pg_hba.conf

5.启动 / 重启 postgreSql

# ./bin/pg_ctl
# 启动数据库
pg_ctl -D /data start
# 重启数据库
pg_ctl restart –D data/ -l log/logfile
# 停止服务器
pg_ctl -D /data/directory stop

6.查看进程

ps -ef | grep pgsql
#或者
ps -ef | grep postgres

7.配置环境变量

export PATH=/usr/local/postgresql/bin:$PATH
export PGHOME=/usr/local/postgresql
export PGDATA=/usr/local/postgresql/data/
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGHOME/lib/
export PATH=$PGHOME/bin:$PATH:$HOME/bin

8.配置服务

# 1. 在***/etc/systemd/system/***目录下,新建.service文件。样例:postgresql-17.service

[Unit]
Description=PostgreSQL 10 database server
After=network.target

[Service]
Type=forking

User=postgres
Group=postgres

# 路径可能需要根据实际安装情况进行调整  
# 确保ExecStart和ExecStop中的路径与你的实际PostgreSQL安装路径相匹配。
ExecStart=/usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data
ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/data -s -m fast

[Install]
WantedBy=multi-user.target

# 2. 保存.service文件后,重新加载systemd管理器配置
sudo systemctl daemon-reload
# 3. 将pgsql服务设置为开机自启动:
sudo systemctl enable postgresql-17.service
# 4.启动服务:
sudo systemctl start postgresql-17.service
# 4.重启服务
sudo systemctl restart postgresql-17.service

9.配置数据库远程连接

pg_hba.conf文件配置 所在目录: /opt/postgresql/pgsql/data/

添加 host all all all trust

# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust
# 允许所有主机连接数据库trust 是免密码 md5 是md5加密
host    all                  all                  0.0.0.0/0               trust
host    all                  all                  all                     trust

postgresql.conf文件 配置 所在目录: /opt/postgresql/pgsql/data/

#listen_addresses = '*'
listen_addresses = '*'

10.配置防火墙

## 添加5432 端口设置防火墙白名单
firewall-cmd --zone=public --add-port=5432/tcp --permanent
## 使配置端口生效
firewall-cmd --reload
## 重启防火墙
systemctl restart firewalld.service

## 防火墙相关的其他命令
# 1.查看防火墙状态
systemctl status firewalld
# firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
# 2.查看防火墙是否开机启动
systemctl is-enabled firewalld
# 3.关闭防火墙
systemctl stop firewalld
systemctl stop firewalld.service
systemctl status firewalld
# 4.禁用防火墙(系统启动时不启动防火墙服务)
systemctl disable firewalld
systemctl disable firewalld.service
systemctl is-enabled firewalld
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜡台

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值