目录
一、下载 PostgreSQL 源码
下载地址:https://www.postgresql.org/ftp/source/v13.1/
下载完成后,上传到 CentOS设备上,并解压,解压命令如下:
tar xzf postgresql-13.1.tar.gz
二、编译 PostgreSQL
使用 tar 命令解压后,会在当前目录下出现目录 postgresql-13.1
进入目录postgresql-13.1,并执行配置脚本
$ cd postgresql-13.1
$./configure --prefix=/opt/common/postgresql
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether NLS is wanted... no
checking for default port number... 5432
checking for block size... 8kB
checking for segment size... 1GB
checking for WAL block size... 8kB
checking for gcc... no
checking for cc... no
configure: error: in `/opt/build/postgresql-13.1':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
结果报错了,错误信息是没有安装 gcc,所以需要安装 gcc
$ yum -y install gcc
安装 gcc 后再次运行 configure 进行配置
$ ./configure --prefix=/opt/common/postgresql
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether NLS is wanted... no
checking for default port number... 5432
checking for block size... 8kB
checking for segment size... 1GB
checking for WAL block size... 8kB
checking for gcc... gcc
checking whether the C compiler works... yes
.
.
.
checking for library containing readline... no
configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure. It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.
这次报错信息是没有找到 readline 库,所以需要安装 readline 库
$ yum -y install readline-devel
安装 readline-devel 后,再次运行 configure 进行配置,缺哪个依赖库就安装哪个库。重复执行 configure,直到 configure 命令运行成功,运行成功后,会在最后输出如下信息:
configure: using compiler=gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
configure: using CFLAGS=-Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2
configure: using CPPFLAGS= -D_GNU_SOURCE
configure: using LDFLAGS= -Wl,--as-needed
configure: creating ./config.status
config.status: creating GNUmakefile
config.status: creating src/Makefile.global
config.status: creating src/include/pg_config.h
config.status: creating src/include/pg_config_ext.h
config.status: creating src/interfaces/ecpg/include/ecpg_config.h
config.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.s
config.status: linking src/backend/port/posix_sema.c to src/backend/port/pg_sema.c
config.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port
configure 执行成功后,就可以进行编译了,编译命令如下:
$ make && make install
如果报错说 make 命令找不到,那就安装 make
yum install -y make
三、配置 PATH 环境变量
编译并安装完成后, PostgreSQL 会安装到 /opt/common/postgresql 目录中,这是因为我们在执行 configure 命令的时候,指定的 --prefix 参数的值是 /opt/common/postgresql,也就是把 PostgreSQL 安装到 /opt/common/postgresql/ 中。但是这个目录没有在系统变量 PATH 中,所以执行 PostgreSQL 的命令(例如 psql)的时候,系统是找不到命令在哪里的,所以需要配置系统变量 PATH,修改方式如下:
临时修改,退出 shell 后,这个配置就消失了,下次使用的时候需要再次执行:
export PATH=/opt/common/postgresql/bin:$PATH
永久修改,修改 ~/.bashrc 或者 /etc/bashrc ,在文件的最后一行添加如下信息:
export PATH=/opt/common/postgresql/bin:$PATH
修改完成后,需要加载一下:
source ~/.bashrc # 或者 source /etc/bashrc
测试 psql 命令:
$ psql --help
psql is the PostgreSQL interactive terminal.
Usage:
psql [OPTION]... [DBNAME [USERNAME]]
General options:
-c, --command=COMMAND run only single command (SQL or internal) and exit
-d, --dbname=DBNAME database name to connect to (default: "root")
-f, --file=FILENAME execute commands from file, then exit
-l, --list list available databases, then exit
-v, --set=, --variable=NAME=VALUE
set psql variable NAME to VALUE
(e.g., -v ON_ERROR_STOP=1)
-V, --version output version information, then exit