CentOS安装PostgreSQL14

文章目录

    • 1. 设置RPM仓库
    • 2. 安装PostgreSQL-Server、PostgreSQL-Contrib
    • 3. 自定义数据存储目录
      • 3.1 创建数据存储目录,并修改权限
      • 3.2 初始化数据库
      • 3.3 修改启动脚本
    • 4. 设置监听地址
    • 5. 新增密码认证
    • 6. 修改postgres超级用户密码

1. 设置RPM仓库

进入官网Red Hat安装教程页面;选择需要安装的PostgreSQL版本、操作系统平台与版本、CPU架构,然后执行生成的安装脚本中的第一行命令,如下:

CentOS安装PostgreSQL14_第1张图片

执行命令:

yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

2. 安装PostgreSQL-Server、PostgreSQL-Contrib

执行命令:

yum install -y postgresql14-server postgresql14-contrib

3. 自定义数据存储目录

3.1 创建数据存储目录,并修改权限

执行命令:

# 创建目录
mkdir -p /usr/local/postgresql/data

# 授权, postgres用户与组在第2步会自动创建好
chown -R postgres:postgres /usr/local/postgresql

3.2 初始化数据库

首先切换到postgres用户,执行命令:

su - postgres

运行初始化数据库脚本

# 使用自定义数据存储目录
/usr/pgsql-14/bin/initdb -D /usr/local/postgresql/data/

# 使用默认数据存储目录
/usr/pgsql-14/bin/initdb

3.3 修改启动脚本

提醒:使用root用户

当使用自定义数据存储目录才需要修改,否则不需要修改;

编辑系统PostgreSQL启动脚本

vim /usr/lib/systemd/system/postgresql-14.service

修改环境变量中PGDATA变量名,将值修改为第3.1步中创建的目录(即自定义数据存储目录)

......
# Location of database directory
Environment=PGDATA=/usr/local/postgresql/data/
......

重新加载系统服务

systemctl daemon-reload

4. 设置监听地址

提醒:使用postgres用户

修改数据存储目录下的postgresql.conf文件(可使用命令查找:find / -name postgresql.conf)

vim /usr/local/postgresql/data/postgresql.conf

将listen_addresses配置的值改为*

......
# - Connection Settings -
listen_addresses = '*'                  # what IP address(es) to listen on;
......

5. 新增密码认证

提醒:使用postgres用户

修改数据存储目录下的pg_hba.conf文件

vim /usr/local/postgresql/data/pg_hba.conf

在文件末尾添加一行设置,如下:

......
# TYPE  DATABASE        USER            ADDRESS                 METHOD
# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 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
# 添加可通过密码对所有用户和主机进行认证
host    all             all             0.0.0.0/0               password

6. 修改postgres超级用户密码

提醒:使用postgres用户

登录PostgreSQL,使用操作系统用户

psql -p 5432 -U postgres

修改postgres超级用户密码

ALTER USER postgres WITH PASSWORD '123456';

你可能感兴趣的:(PostgreSQL,postgresql,数据库)