文章目录

引言(Introduction)Step0: 屏蔽原来的pg命令路径(Optional)Step0Plus: 检查是否包含要求的软件包Step1: 下载源码Step2:创建新目录Step3: 配置项目Step4: make与make installStep5: 验证Step6: 创建数据库集群Step7: 运行postgresStep8: 执行psqlStep9: 用GDB调试postmasterStep10: 退出postgres参考资料(References)

引言(Introduction)

因为之前安装的PG是14.0版本的,为了跟随社区的步伐以及更好地学习PG相关的知识,我特意重新安装了最新版本的PG15.2(2023.04.04)。特地写一篇文章记录安装的过程,本人的系统是Ubuntu20.04。

Step0: 屏蔽原来的pg命令路径(Optional)

写在前面,如果之前没有安装过PG,那么可以跳过这一步。

由于我原来安装了PG14.0,因此这里需要屏蔽该版本的所有二进制命令,这里通过重命名原来的命令文件夹的方法进行屏蔽。比如说我存放二进制命令的文件夹路径为"/usr/local/postgresql/",那么只需要变更其名称即可:

mv /usr/local/postgresql /usr/local/postgresql.old # use your path here

或者通过修改PATH屏蔽,这里不介绍这种方法。

Step0Plus: 检查是否包含要求的软件包

PG可以运行在大多数的类Unix系统上。在构建PG之前,需要确保系统中具有要求的软件包。本人在刚安装的Ubuntu20.04上还需要另外安装的包包括:

1. build-essential

2. libreadline-dev

3. zlib1g zlib1g-dev

4. bison

5. flex

6. libpq-dev

这些包能不通过源码安装就不必通过源码安装。

Step1: 下载源码

这里可以通过两个方法下载源码,一个是通过官网上的FTP服务器下载tar文件(速度快),第二个方法是通过git下载(速度慢)。本文采用的方法是通过源码安装:

wget -i https://ftp.postgresql.org/pub/source/v15.2/postgresql-15.2.tar.gz

下载后解压得到文件夹postgresql-15.2。

tar -zxvf postgresql-15.2.tar.gz

或者通过git下载:

git clone https://git.postgresql.org/git/postgresql.git

得到文件夹postgresql后,创建自己的分支(便于修改源码)。clone之后默认存在master分支。

cd postgresql

git checkout -b your-branch

Step2:创建新目录

这一步主要创建存储数据库集群的目录:

mkdir /home/yourname/15.2 # directory for build

mkdir /home/yourname/DATA # directory for cluster

Step3: 配置项目

源码postgresql文件中包含了可运行的配置文件configure,可以通过–help参数查看帮助文档,正常模式下执行configure

./configure --prefix=/home/yourname/15.2

或者加入调试设置:

./configure --prefix=/home/yourname/15.2 --enable-depend --enable-debug --enable-cassert CFLAGS=-O0

Step4: make与make install

在源码文件夹postgresql中执行make与make install

make && make install

这里可能会报很多Wall,但是如果不是缺少包的,就没有必要管。

Step5: 验证

在make install之后,会在前面configure指定的路径中生成几个文件夹bin,share几个文件夹,验证是否安装成功可以运行bin下面的pg_config:

cd /home/yourname/15.2/bin

./pg_config

如果有配置信息,那么安装成功。为了方便,可以将该bin路径添加到PATH变量中,笔者已经添加了,所以这里不加以描述。

Step6: 创建数据库集群

在刚刚的bin目录以及创建的DATA文件创建集群:

./initdb -D /home/yourname/DATA

Step7: 运行postgres

./pg_ctl -D /home/yourname/DATA start

此时使用ps查看postgres的进程状态:

ps ux | grep postgres

Step8: 执行psql

执行psql本地连接postgres服务:

psql

如果前面没有创建用户postgres,那么会报没有数据库的错误,这个时候需要指定数据库的名称为postgres。连接postgres后创建一个新的数据库,名字为你当前的用户名,这样以后默认连接到你名字的数据库。

psql postgres

postgres=# create database yourname;

postgres=# \c yourname;

yourname=#

Step9: 用GDB调试postmaster

如果前面在配置项目阶段设置了–enable-debug,那么可以通过gdb对postgres进行调试。另外,因为调试的时候需要多个窗口,这里推荐使用tmux工具。首先启动postgres进程,然后执行psql:

# terminator 1

pg_ctl -D /home/yourname/DATA start

psql

然后在另外一个窗口查看进程号,同时使用gdb attach该进程。

# terminator 2

ps ux | grep postgres

sudo gdb attach yourPID

注意这里attach的进程是Postmaster分配给用户的后台进程,是带有idle空闲字样的进程:

在attach之后整个系统都会暂停,这个时候你可以打断点,然后continue(一定要继续程序才能正常调试),比如说:

# terminator 2

b exec_simple_query # set break point

c # continue

然后在窗口1中执行查询命令:

# terminator 1

\d

这个时候就会看到窗口2中出现了中断数据:

Step10: 退出postgres

pg_ctl -D /home/yourname/DATA stop

参考资料(References)

Getting the source

Requirements

Working with Git

InsInstallation from Source Code

Working with Git

InsInstallation from Source Code

install-postgresql-using-source-code

好文阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: