PostgreSQL 快速安装
# 一、准备环境
CloudBeaver 需要一个外部数据库保存用户信息和连接配置。默认推荐使用 PostgreSQL,本文将以 CentOS 7 环境为例完成安装。
默认用户:postgres 默认数据库:postgres 默认密码:postgres_pwd# 二、PostgreSQL 安装步骤
# 2.1 添加 Yum 源(阿里云)
cat >/etc/yum.repos.d/pgdg.repo <<EOF
[pgdg15]
name=PostgreSQL 15
baseurl=https://mirrors.aliyun.com/postgresql/repos/yum/15/redhat/rhel-7-x86_64
enabled=1
gpgcheck=0
EOF
1
2
3
4
5
6
7
2
3
4
5
6
7
# 2.2 安装服务端与客户端工具
yum clean all && yum makecache
yum install -y postgresql15 postgresql15-server
1
2
2
# 2.3 初始化数据库
/usr/pgsql-15/bin/postgresql-15-setup initdb
1
# 三、服务启动与远程访问配置
# 3.1 启动 PostgreSQL 并设置开机自启
systemctl enable postgresql-15
systemctl start postgresql-15
1
2
2
# 3.2 修改配置允许远程连接
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/" /var/lib/pgsql/15/data/postgresql.conf
echo "host all all 0.0.0.0/0 md5" >> /var/lib/pgsql/15/data/pg_hba.conf
1
2
2
警告
该配置允许所有主机访问,请在防火墙和网络层限制来源 IP,避免暴露于公网。
# 3.3 重启服务应用配置
systemctl restart postgresql-15
1
# 四、设置默认 postgres
密码
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'postgres_pwd';"
1
提示
该用户为 CloudBeaver 默认连接使用,确保密码与 CloudBeaver 配置保持一致。
# 五、一键部署脚本(方便快捷)
你可以使用以下脚本自动完成 PostgreSQL 安装和配置:
#!/bin/bash
set -e
PG_VERSION=15
PG_DATA_DIR="/var/lib/pgsql/${PG_VERSION}/data"
PG_PASSWORD="postgres_pwd"
echo ">>> 添加 Yum 源"
cat >/etc/yum.repos.d/pgdg.repo <<EOF
[pgdg${PG_VERSION}]
name=PostgreSQL ${PG_VERSION}
baseurl=https://mirrors.aliyun.com/postgresql/repos/yum/${PG_VERSION}/redhat/rhel-7-x86_64
enabled=1
gpgcheck=0
EOF
yum clean all && yum makecache
yum install -y postgresql${PG_VERSION} postgresql${PG_VERSION}-server
echo ">>> 初始化数据库"
"/usr/pgsql-${PG_VERSION}/bin/postgresql-${PG_VERSION}-setup" initdb
echo ">>> 启动并设置开机自启"
systemctl enable postgresql-${PG_VERSION}
systemctl start postgresql-${PG_VERSION}
echo ">>> 修改远程访问配置"
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/" "${PG_DATA_DIR}/postgresql.conf"
echo "host all all 0.0.0.0/0 md5" >> "${PG_DATA_DIR}/pg_hba.conf"
systemctl restart postgresql-${PG_VERSION}
echo ">>> 设置 postgres 密码"
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD '${PG_PASSWORD}';"
echo "✅ PostgreSQL 安装完成,可用于 CloudBeaver"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35