Linux 安装教程
PostgreSQL 官方网站:https://www.postgresql.org/
1. 点击官网,进入下载页面
2. 选择适合版本
有两种安装方式(一、社区 yum 安装 二、源码包编译安装)。生产环境下更推荐编译安装,本文由于是测试教学,所以在 yum 安装下执行操作。
使用的是 Linux CentOS 7 系统。
3. 获取下载命令
选择好自己的系统,版本号以及你所想下载的 PostgreSQL 版本号,就可以直接获取社区 yum 下载命令,直接复制至自己的虚拟机内即可下载。
# Install the repository RPM: sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# Install PostgreSQL: sudo yum install -y postgresql14-server
# Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
# 初始化 pgsql
sudo systemctl enable postgresql-14
# 设置开机自启动
sudo systemctl start postgresql-14
4. 修改 postgres 账号密码
PostgreSQL 安装成功之后,会默认创建一个名为 postgres 的 Linux 用户,初始化数据库后,会有名为 postgres 的数据库,来存储数据库的基础信息(如用户信息等等),相当于 MySQL 中默认的名为 mysql 的数据库。
postgres 数据库中会初始化一名超级用户 postgres。
为了方便我们使用 postgres 账号进行管理,我们可以修改该账号的密码。
4.1 进入 PostgreSQL 命令行
通过 su 命令切换 linux 用户为 postgres 会自动进入命令行。
su - postgres
4.2 启动 SQL shell
psql
4.3 修改密码
alter user postgres with password 'NewPassword';
提示密码修改成功。
4.4 配置远程访问
firewall-cmd --add-port=5432/tcp --permanent
firewall-cmd --reload
4.5 修改 IP 绑定
# 修改配置文件
vim /var/lib/pgsql/14/data/postgresql.conf
listen_address=
vim /var/lib/pgsql/14/data/pg_hba.conf
host all all 0.0.0.0/0 md5
systemctl restart postgresql-14

