本文简要介绍如何在 CentOS 上搭建 PostgreSQL 开发环境,以及如何编译 PostgreSQL 数据库。

安装依赖

由于可能使用不同的 GCC 进行编译,因此我们需要管理 GCC 的版本,好在 CentOS 提供了这样的工具,我们可以使用下面的命令来安装不同的 GCC。

$ sudo yum install -y centos-release-scl
$ sudo yum install -y devtoolset-10

安装完成之后,查看 GCC 版本,如下所示:

$ gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

我们可以通过以下的命令来切换 GCC 版本。

$ scl enable devtoolset-10 bash
$ gcc --version
gcc (GCC) 10.2.1 20210130 (Red Hat 10.2.1-11)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

编译器已经安装好了,接下来需要安装 PostgreSQL 的依赖。

$ sudo yum install -y libicu-devel
$ sudo yum install -y zlib-devel.x86_64
$ sudo yum install -y openssl-devel.x86_64
$ sudo yum install -y python3-devel.x86_64
$ sudo yum install -y perl-Test-Harness-3.28-3.el7.noarch
$ sudo yum install -y perl-tests.x86_64
$ sudo yum install -y perl-IPC-Run.noarch

编译

经过上面的步骤,我们已经安装好了所需的依赖,接下来可以编译数据库。根据个人的需求,您可能有不同的编译选项(可能需要安装对应的依赖),本文给出一个简单的编译示例。

$ ../configure --prefix=$PWD/pg --enable-rpath --enable-thread-safety \
  --enable-nls --enable-debug --enable-tap-tests --with-icu --with-openssl \
  --with-perl --with-python CFLAGS='-ggdb -O0'
$ make -j 32 world
$ make install -s world
$ make check-world

此外,为了方便查看文档,我们可以安装 man-pages

$ sudo yum install -y man-pages

HunghuDB 依赖

sudo yum install -y centos-release-scl
sudo yum install -y devtoolset-11
sudo yum install -y git

sudo yum install -y zlib-devel.x86_64
sudo yum install -y readline-devel.x86_64

sudo yum install -y devtoolset-11-systemtap-sdt-devel  # --enable-dtrace
sudo yum install -y llvm-toolset-7-llvm-devel.x86_64   # --with-llvm
sudo yum install -y libicu-devel                       # --with-icu
sudo yum install -y numactl-devel.x86_64               # --with-numa
sudo yum install -y lz4-devel.x86_64                   # --wit-lz4
sudo yum install -y openssl-devel.x86_64               # --with-openssl
sudo yum install -y python3-devel.x86_64               # --with-python
sudo yum install -y pam-devel.x86_64                   # --with-pam
sudo yum install -y libxml2-devel.x86_64               # --with-libxml
sudo yum install -y krb5-devel.x86_64                  # --with-gssapi
sudo yum install -y libxslt-devel.x86_64               # --with-libxslt
sudo yum install -y openldap-devel.x86_64              # --with-ldap
sudo yum install -y libuuid-devel.x86_64               # --with-uuid=e2fs
sudo yum install -y systemd-devel.x86_64               # --with-systemd
sudo yum install -y llvm-toolset-7.0.x86_64            # --with-llvm
sudo yum install -y llvm-toolset-7.0-llvm-devel.x86_64

# regression test
sudo yum install -y perl-Test-Harness-3.28-3.el7.noarch
sudo yum install -y perl-tests.x86_64
sudo yum install -y perl-IPC-Run.noarch

# make a RPM package
sudo yum install -y rpmdevtools.noarch

默认情况下,git 版本有点老了,可以使用下面的命令安装新版本的 git。

sudo yum -y remove git
sudo yum -y install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm
sudo yum -y install git

参考

[1] https://linuxize.com/post/how-to-install-gcc-compiler-on-centos-7/ [2] https://computingforgeeks.com/how-to-install-latest-version-of-git-git-2-x-on-centos-7/

附录

依赖安装汇总如下所示。

sudo yum install -y centos-release-scl
sudo yum install -y devtoolset-10
sudo yum install -y libicu-devel
sudo yum install -y zlib-devel.x86_64
sudo yum install -y openssl-devel.x86_64
sudo yum install -y python3-devel.x86_64
sudo yum install -y perl-Test-Harness-3.28-3.el7.noarch
sudo yum install -y perl-tests.x86_64
sudo yum install -y perl-IPC-Run.noarch

如果是不能使用 yum 方式安装 perl-IPC-RUN,我们还可以使用 cpan 进行安装。

$ cpan
cpan[1]> install IPC::Run

通常情况下,我建议使用虚拟路径的方式来编译 PostgreSQL,下面是我惯用的编译模式。

$ cd postgres
$ mkdir build && cd build
$ cat <<END > compile.sh
rm -rf \$(ls -I '*.sh')
cat <<EOF | scl enable devtoolset-10 bash
../configure --prefix=\$PWD/pg \\
  --enable-rpath --enable-thread-safety --enable-nls --enable-debug \\
  --enable-tap-tests \\
  --with-icu --with-openssl --with-perl --with-python \\
  && \\
make -j 32 world && make install -s world && make check-world
EOF
END
$ chmod +x compile.sh