查看阿里云服务器版本

cat /etc/centos-release

安装

# Install the repository RPM:
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Disable the built-in PostgreSQL module:
sudo dnf -qy module disable postgresql

# Install PostgreSQL:
sudo dnf install -y postgresql15-server

# Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
sudo systemctl enable postgresql-15
sudo systemctl start postgresql-15

安装过程中可能出现的异常情况

  • 安装过程中,如果遇到如下报错
Status code: 404 for https://download.postgresql.org/pub/repos/yum/common/redhat/rhel-3-x86_64/repodata/repomd.xml
Error: Failed to download metadata for repo 'pgdg-common': Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried

处理方法

# 该目录下有 pgdg-common 的 repo,可能的名称:pgdg-redhat-all.repo
cd /etc/yum.repos.d
# 将上面目录中 pgdg-common.repo 的名称复制到下面的文件中,追加到 include 最后面
vim /etc/yum/pluginconf.d/releasever_adapter.conf
# 如下一行
# include=docker-ce.repo, epel.repo, pgdg-redhat-all.repo

登录修改默认密码

# 切换至 postgres 用户
su - postgres
# 登录数据库
psql
# 在数据库客户端执行修改密码命令
ALTER USER postgres WITH PASSWORD 'postgres';
# 退出客户端
\q

配置远程登录

配置 pg_hba.conf

vim /var/lib/pgsql/15/data/pg_hba.conf

增加一行

host    all             all             0.0.0.0/0               scram-sha-256
vim /var/lib/pgsql/15/data/postgresql.conf

找到相应位置修改

listen_addresses = '*'
port = 5432
max_connections = 2000

重启服务

sudo systemctl restart postgresql-15

还需要在阿里云为实例配置安全组,开放 postgresql 端口

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐