103 lines
3.0 KiB
Bash
103 lines
3.0 KiB
Bash
#!/bin/bash
|
||
|
||
# PostgreSQL 14 安装配置脚本 (Rocky Linux 8.0)
|
||
# 数据库将安装在 /data/postgresql
|
||
# 账号: 1825713423 密码: hsc1825713423
|
||
|
||
# 检查是否为root用户
|
||
if [ "$(id -u)" -ne 0 ]; then
|
||
echo "请使用root用户运行此脚本!"
|
||
exit 1
|
||
fi
|
||
|
||
# 1. 添加PostgreSQL官方仓库
|
||
echo "添加PostgreSQL官方YUM仓库..."
|
||
dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
|
||
dnf -qy module disable postgresql
|
||
|
||
# 2. 安装PostgreSQL 14
|
||
echo "安装PostgreSQL 14..."
|
||
dnf install -y postgresql14-server postgresql14-contrib
|
||
|
||
# 3. 创建数据目录并初始化
|
||
echo "创建数据目录并初始化数据库..."
|
||
mkdir -p /data/postgresql
|
||
chown postgres:postgres /data/postgresql
|
||
|
||
sudo -u postgres /usr/pgsql-14/bin/initdb -D /data/postgresql
|
||
|
||
# 4. 修改配置文件
|
||
echo "配置PostgreSQL..."
|
||
cat > /data/postgresql/postgresql.conf <<EOF
|
||
listen_addresses = '*'
|
||
port = 5432
|
||
max_connections = 100
|
||
shared_buffers = 128MB
|
||
dynamic_shared_memory_type = posix
|
||
datestyle = 'iso, mdy'
|
||
timezone = 'Asia/Shanghai'
|
||
log_timezone = 'Asia/Shanghai'
|
||
unix_socket_directories = '/var/run/postgresql'
|
||
password_encryption = scram-sha-256
|
||
EOF
|
||
|
||
# 5. 配置客户端认证
|
||
cat > /data/postgresql/pg_hba.conf <<EOF
|
||
# TYPE DATABASE USER ADDRESS METHOD
|
||
local all all scram-sha-256
|
||
host all all 127.0.0.1/32 scram-sha-256
|
||
host all all ::1/128 scram-sha-256
|
||
host all all 0.0.0.0/0 scram-sha-256
|
||
EOF
|
||
|
||
# 6. 创建系统服务
|
||
cat > /etc/systemd/system/postgresql-14.service <<EOF
|
||
[Unit]
|
||
Description=PostgreSQL 14 database server
|
||
After=network.target
|
||
|
||
[Service]
|
||
Type=forking
|
||
User=postgres
|
||
Group=postgres
|
||
Environment=PGDATA=/data/postgresql
|
||
OOMScoreAdjust=-1000
|
||
ExecStart=/usr/pgsql-14/bin/pg_ctl -D \${PGDATA} start
|
||
ExecStop=/usr/pgsql-14/bin/pg_ctl -D \${PGDATA} stop
|
||
ExecReload=/usr/pgsql-14/bin/pg_ctl -D \${PGDATA} reload
|
||
TimeoutSec=0
|
||
|
||
[Install]
|
||
WantedBy=multi-user.target
|
||
EOF
|
||
|
||
# 7. 启动PostgreSQL服务
|
||
echo "启动PostgreSQL服务..."
|
||
systemctl daemon-reload
|
||
systemctl enable postgresql-14
|
||
systemctl start postgresql-14
|
||
|
||
# 8. 创建指定用户和密码
|
||
echo "创建用户和数据库..."
|
||
sudo -u postgres psql <<EOF
|
||
CREATE USER "1825713423" WITH PASSWORD 'hsc1825713423';
|
||
ALTER USER "1825713423" WITH SUPERUSER;
|
||
CREATE DATABASE db_1825713423 OWNER "1825713423";
|
||
GRANT ALL PRIVILEGES ON DATABASE db_1825713423 TO "1825713423";
|
||
EOF
|
||
|
||
# 9. 开放防火墙
|
||
echo "配置防火墙..."
|
||
firewall-cmd --add-port=5432/tcp --permanent
|
||
firewall-cmd --reload
|
||
|
||
# 10. 完成信息
|
||
echo "PostgreSQL 14 安装配置完成!"
|
||
echo "连接信息:"
|
||
echo "主机: localhost"
|
||
echo "端口: 5432"
|
||
echo "用户名: 1825713423"
|
||
echo "密码: hsc1825713423"
|
||
echo "默认数据库: db_1825713423"
|
||
echo "数据目录: /data/postgresql"
|
||
echo "管理命令: systemctl [start|stop|restart] postgresql-14" |