PostgreSQL 14 安装与卸载(CentOS 7)

本贴最后更新于 580 天前,其中的信息可能已经水流花落

PostgreSQL 是以加州大学伯克利分校计算机系开发的 POSTGRES, 版本 4.2 为基础的对象关系型数据库管理系统(ORDBMS)。

PostgreSQL 有两种安装方式:

  • 通过二进制文件安装
  • 通过源码构建安装

PostgreSQL 推荐采用第一种方式,并且为常见的操作系统提供了二进制安装文件。从源代码构建的方式仅推荐在开发 PostgreSQL 或扩展的时候使用。

通过二进制文件安装

创建 postgres 用户

useradd postgres
usermod -G wheel postgres
echo "123" | passwd --stdin postgres

PostgreSQL 软件安装

1、通过 yum 源安装

我这里使用的是 CentOS Linux release 7.9.2009 (Core) 操作系统。在 PostgreSQL: Linux downloads (Red Hat family) 页面可以找到对应的安装命令。

使用 postgres 用户执行下面的命令:

# 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

如果使用其他非 root 用户进行安装,并且 postgres 用户不存在,安装程序会为 PostgreSQL 服务创建一个 postgres 操作系统用户,在 /etc/passwd 可以查到:

[wzk@localhost ~]$ cat /etc/passwd
.......
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
[wzk@localhost ~]$

2、使用 rpm 包安装

如果服务器不能连接外网,可以使用该种方式进行安装。

下载 PostgreSQL rpm 安装包:postgresql-client、postgresql-server、postgresql-contrib 和 postgresql-devel,并将下载的 rpm 包上传到服务器。

切换到 rpm 包所在的目录,使用 postgres 用户执行下面的命令:

sudo yum install -y postgresql14-*

PostgreSQL 初始化

上一步骤只是安装了 PostgreSQL 软件,接下来进行初始化,来创建一个数据库集群。

数据库集群是由一个服务器实例管理的数据库集合。

同样地,使用 postgres 用户登录服务器。

配置环境变量:

echo 'export PGDATA=/var/lib/pgsql/14/data' >> ~/.bash_profile
echo 'export PATH=$PATH:/usr/pgsql-14/bin' >> ~/.bash_profile
source ~/.bash_profile

执行下面的初始化命令:

sudo postgresql-14-setup initdb

该命令将会创建一个新的 PostgreSQL 数据库集群。在这个过程中,initdb 将会:

  • 创建数据库的数据目录,默认是:/var/lib/pgsql/14/data
  • 生成共享的 catalog 表(属于整个数据库集群)。
  • 创建 template1 数据库,这是一个模板数据库。当新建一个数据库时,将会复制 template1 中的内容。
  • 创建 postgres 数据库 (供用户、实用程序和第三方应用使用的默认数据库)。
  • 基于操作系统环境设置默认地区和字符编码。

如何修改默认的数据库数据目录?

若要修改默认的数据库目录,则需要在执行 postgresql-14-setup initdb 命令之前,执行下面命令:

sudo systemctl edit postgresql-14.service

添加下面的内容:

[Service]
Environment=PGDATA=/home/postgres/data

这个命令会创建 /etc/systemd/system/postgresql-14.service.d/override.conf 文件,该文件的内容会合并到原始的服务文件中。

不要忘了创建指定的数据库目录(用户):

mkdir /home/postgres/data

PostgreSQL 服务启动

启动数据库服务并设置开机自启动

sudo systemctl start postgresql-14
sudo systemctl enable postgresql-14

通过源码构建安装

1、创建 postgres 用户

useradd postgres
usermod -G wheel postgres
echo "123" | passwd --stdin postgres

2、安装依赖包

默认的 CentOS 软件源上包含一个名称为 Development Tools 的软件包组,这个组合包含了 GCC 编译器以及一系列库文件,还有其他编译软件需要用到的工具。基本上可以满足 PostgreSQL 的编译要求。

sudo yum group install -y y"Development Tools"
sudo yum install -y wget readline-devel zlib-devel

3、使用 postgres 用户下载源码,并解压

wget --no-check-certificate https://ftp.postgresql.org/pub/source/v14.5/postgresql-14.5.tar.gz
tar xvf postgresql-14.5.tar.gz

4、切换到解压后的目录,构建安装

cd postgresql-14.5
./configure
make
su
make install

5、数据库初始化

用 root 用户创建数据库数据目录:

mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data

切换到 postgres 用户,初始化数据库集群:

su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data

6、启停数据库服务

# 启动
/usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data -l logfile
# 停止
/usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/data -l logfile

7、配置环境变量

echo 'export PGDATA=/usr/local/pgsql/data' >> ~/.bash_profile
echo 'export PATH=$PATH:/usr/local/pgsql/bin' >> ~/.bash_profile
source ~/.bash_profile

数据库连接

1、本地连接

服务启动成功后,可以使用 psql 客户端工具本地连接数据库。

[postgres@localhost ~]$ psql
psql (14.5)
Type "help" for help.

postgres=# select version();
                                                 version
---------------------------------------------------------------------------------------------------------
 PostgreSQL 14.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
(1 row)

2、远程连接

若要远程连接数据库,则需要修改数据目录下的配置文件。

首先,修改 postgresql.conf 文件,将 listen_addresses 取消注释,并设置 IP 值。比如:

listen_addresses = '*' # * 代表监听所有IP,也可以指定特定的 IP 列表

修改此参数需要重启数据库服务:

sudo systemctl restart postgresql-14

然后,修改 pg_hba.conf 文件,配置用户权限。比如:

# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    all             foo             0.0.0.0/0               scram-sha-256 # foo 用户可以通过任意 IP 以 scram-sha-256 加密的方式登录,可以访问所有数据库

修改此配置文件需要重新加载:

pg_ctl reload

最后,用 psql 连接数据库创建 foo 用户并设置密码:

CREATE USER foo PASSWORD '123';

配置完成后就可以使用 pgAdmin 等工具远程访问数据库了。

卸载 PostgreSQL

通过二进制文件安装的,使用下面的命令卸载:

yum remove postgresql*
rm -rf  /usr/pgsql*
rm -rf /var/lib/pgsql #数据文件目录根据实际情况指定
userdel -r postgres

通过源码构建安装的,使用下面的命令卸载:

rm -rf /usr/local/pgsql
userdel -r postgres

相关资料

PostgreSQL: Linux downloads (Red Hat family)

PostgreSQL: Documentation: 14: initdb

postgresql-setup --initdb with custom data directory

PostgreSQL Database Server 14 PGDG

PostgreSQL: Documentation: 14: Chapter 17. Installation from Source Code

  • PostgreSQL

    PostgreSQL 是一款功能强大的企业级数据库系统,在 BSD 开源许可证下发布。

    22 引用 • 22 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...