PostgreSQL修炼之道:从小工到专家. 2.2 从源码安装

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介:

2.2 从源码安装

2.2.1 编译安装过程介绍

这里先把大致的安装过程介绍一下。

第一步:下载源代码。

第二步:编译安装。过程与Linux下其他软件的编译安装过程相同,都是“三板斧”:

./configure

make

make install

第三步:编译安装完成后执行如下步骤。

1)使用initdb命令初使用化数据库簇。

2)启动数据库实例。

3)创建一个应用使用的数据库。

数据库簇是指数据库实例管理的系统文件和各个数据库文件的一个集合。

2.2.2 下载源代码

打开PostgreSQL的官方网站www.postgresql.org,如图2-18所示。

 

图2-18 PostgreSQL官方网站界面

点击网站菜单中的download,进入下载页面,如图2-19所示。

 

图2-19 PostgreSQL官方网站中的源代码下载界面

在下载页面中点左侧的“Source”,进入源代码下载页面,如图2-20所示。

 

图2-20 PostgreSQL官方源码下载中的选择版本界面

在源代码页面中选择合适的版本,比如v9.2.4,如图2-21所示。

 

图2-21 PostgreSQL官方源码下载中的选择v9.2.4版本界面

然后,在上面的页面中选择合适的压缩包下载就可以了,一般选择bz2的压缩包,因为这种格式体积较小。

2.2.3 编译及安装

默认情况下,安装会用到数据库中的压缩功能,而这个功能的实现需要第三方的压缩开发包zlib支持,在不同的Linux发行版本下,此包的名字会不太一样,但包的名字一般都含有“zlib”和“dev”两个字符串,“dev”是“develop”即开发包的意思。如在Ubuntu12.04下,可以使用下面的方法查找包的名称:

osdba@osdba-laptop:~$ aptitude search zlib |grep dev

p   libghc-bzlib-dev                - Haskell bindings to the bzip2 library    

p   libghc-bzlib-dev:i386           - Haskell bindings to the bzip2 library    

v   libghc-bzlib-dev-0.5.0.3-77459: -                                          

v   libghc-bzlib-dev-0.5.0.3-f7d98  -                                          

p   libghc-zlib-bindings-dev        - low-level bindings to zlib               

p   libghc-zlib-bindings-dev:i386   - low-level bindings to zlib               

v   libghc-zlib-bindings-dev-0.1.0. -                                          

v   libghc-zlib-bindings-dev-0.1.0. -                                          

p   libghc-zlib-conduit-dev         - streaming compression/decompression via co

p   libghc-zlib-conduit-dev:i386    - streaming compression/decompression via co

v   libghc-zlib-conduit-dev-0.4.0.1 -                                          

v   libghc-zlib-conduit-dev-0.4.0.1 -                                          

p   libghc-zlib-dev                 - Compression and decompression in the gzip

p   libghc-zlib-dev:i386            - Compression and decompression in the gzip

v   libghc-zlib-dev-0.5.3.3-78ddb:i -                                          

v   libghc-zlib-dev-0.5.3.3-7baa4   -                                          

p   libghc-zlib-enum-dev            - enumerator interface for zlib compression

p   libghc-zlib-enum-dev:i386       - enumerator interface for zlib compression

v   libghc-zlib-enum-dev-0.2.2.1-16 -                                           

v   libghc-zlib-enum-dev-0.2.2.1-f8 -                                          

p   libghc6-bzlib-dev               - transitional dummy package               

p   libghc6-zlib-dev                - transitional dummy package               

p   lua-zlib-dev                    - zlib development files for the Lua languag

p   lua-zlib-dev:i386               - zlib development files for the Lua languag

p   zlib1g-dbg                      - compression library - development        

p   zlib1g-dbg:i386                 - compression library - development        

i A zlib1g-dev                      - compression library - development        

p   zlib1g-dev:i386                 - compression library - development

从上面列出的包来看,只有“zlib1g-dev”的名称与我们需要的zlib开发包最接近,从而确定在Ubuntu12.10上(当然这还需要一些经验)应该安装这个包。

如果想要方便地在psql中使用上下键翻查历史命令,按照PostgreSQL官方手册的说明,还需要安装readline的开发包。与上面的方法类似,先查找包含“readline”和“dev”的包:

osdba@osdba-laptop:~$ aptitude search readline |grep dev

v   lib32readline-dev               -                                          

p   lib32readline-gplv2-dev         - GNU readline and history libraries, develo

p   lib32readline6-dev              - GNU readline and history libraries, develo

v   lib64readline-dev:i386          -                                          

p   lib64readline-gplv2-dev:i386    - GNU readline and history libraries, develo

p   lib64readline6-dev:i386         - GNU readline and history libraries, develo

p   libghc-readline-dev             - Interface to the GNU readline library    

p   libghc-readline-dev:i386        - Interface to the GNU readline library    

v   libghc-readline-dev-1.0.1.0-52b -                                          

v   libghc-readline-dev-1.0.1.0-69f -                                          

i   libreadline-dev                 - GNU readline and history libraries, develo

p   libreadline-dev:i386            - GNU readline and history libraries, develo

p   libreadline-gplv2-dev           - GNU readline and history libraries, develo

p   libreadline-gplv2-dev:i386      - GNU readline and history libraries, develo

i A libreadline6-dev                - GNU readline and history libraries, develo

p   libreadline6-dev:i386           - GNU readline and history libraries, develo

从上面列出的包来看,只有“libreadline6-dev”的名称与我们需要的readline开发包最接近,所以应该安装这个包。

把前面下载的压缩包解压,如果该压缩包名称为postgresql-9.2.4.tar.bz2,解压命令则为:

tar xvf postgresql-9.2.4.tar.bz2

对于PostgreSQL8.X的版本,一般编译安装的第一板斧是使用configure命令,如下:

./configure --prefix=/usr/local/pgsql8.4.17 --enable-thread-safety --with-perl --with-python

对于PostgreSQL9.X的版本,一般编译安装的命令如下:

./configure --prefix=/usr/local/pgsql9.2.4 --with-perl --with-python

对比可以发现,在PostgreSQL8.X中,编译命令里有“--enable-thread-safety”选项,而在PostgreSQL9.X中没有这个选项。因为在日常使用中,一般要求客户端是线程安全的,PostgreSQL9.X考虑到这个问题,默认为线程安全的了,而PostgreSQL8.X没有,所以要加上这个选项。

再看看下面两个选项:

--with-perl: 加上这个选项,才能使用perl语法的PL/Perl过程语言写自定义函数,一般都需要。使用这个选项要求先安装perl开发包,该包在Ubuntu或Debian下的名称为:libperl-dev,可使用apt-get install libperl-dev安装。

--with-python:加上这个选项,才能使用python语法的PL/Python过程语言写自定义函数,一般都需要。使用这个选项要求先安装python-dev开发包,该包在Ubuntu或Debian下的名称为:python-dev,可使用apt-get install python-dev安装。

编译安装的第二板斧是运行make命令,这个命令没有什么好说的,直接运行就可以了:

make

按官方文档要求,使用make命令时,其版本要在gmake3.8以上,目前绝大多数的Linux发行版本都满足要求,所以在Linux下一般不需要检查make的版本,但如果是在其他非Linux的UNIX平台上,还是先检查一下make的版本比较好,命令如下:

osdba@osdba-laptop:~$ make --version

GNU Make 3.81

Copyright (C) 2006  Free Software Foundation, Inc.

在其他的UNIX平台上,有可能存在非GNU的make,这时GNU的make的名称会是gmake。

编译安装的第三板斧是运行make install命令。如果是在一般用户下进行编译的,可能对/usr/local目录没有写的权限,而运行make install命令时是需要使用root权限的,所以在Debian或Ubuntu下可以使用sudo:

sudo make install

在前面我们看到--prefix设置的路径为“/usr/local/pgsql9.2.4”,如果不设置这个路径,默认的路径将是“/usr/local”,为什么要在此路径上加上PostgreSQL的版本号呢?这是为了升级方便。make install命令运行完之后,还要进入/usr/local目录下,为/usr/local/pgsql9.2.4建立一个/usr/local/pgsql的链接:

cd /usr/localL9.2.

sudo ln -sf /usr/local/pgsql9.2.4 /usr/local/pgsql

如果PostgreSQL9.2.5发布了,在编译PostgreSQL9.2.5后,只需把现有的数据库停掉,然后把链接/usr/local/pgsql指向先前的版本/usr/local/pgsql9.2.5即可完成升级。是不是觉得很方便呢?

2.2.4 安装后的配置

安装完后,需要设置PostgreSQL可执行文件的路径:

export PATH=/usr/local/pgsql/bin:$PATH

还要设置共享库的路径:

export LD_LIBRARY_PATH=/usr/local/pgsql/lib

如果想让以上配置对所有的用户生效,可以把以上内容加到/etc/profile文件中,/etc/profile中的内容看起来类似如下:

...

...

...

if [ -d /etc/profile.d ]; then

  for i in /etc/profile.d/*.sh; do

    if [ -r $i ]; then

      . $i

    fi

  done

  unset i

fi

 

 

export PATH=/usr/local/pgsql/bin:$PATH

export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH

如果想让以上配置对当前用户生效,在Linux下可以把以上内容加到.bashrc文件中,在其他UNIX下可以加到.profile文件中。

有人问在Linux下为何不加到.profile文件或.bash_profile文件中,这是因为有时在图形界面下打开一个终端,.profile或.bash_profile不会生效。

2.2.5 创建数据库簇

先设定数据库中数据目录的环境变量:

export PGDATA=/home/osdba/pgdata

执行下面的命令创建数据库簇:

initdb

这样就完成了。

2.2.6 安装contrib目录下的工具

contrib下面有一些工具比较实用,一般都会安装上,其安装方法也与Linux下的编译过程相同,如下:

cd postgresql-9.2.3/contrib

make

sudo make install

2.2.7 启动和停止数据库

启动数据库的命令为:

pg_ctl start -D $PGDATA

其中,环境变量$PGDATA指向具体的PostgreSQL数据库的数据目录,看以下示例:

osdba@osdba-laptop:~$ pg_ctl start -D /home/osdba/pgdata

server starting

停止数据库的命令如下:

pg_ctl stop -D $PGDATA [-m SHUTDOWN-MODE]

其中-m是指定数据库的停止方法,有以下三种:

smart:等所有的连接中止后,关闭数据库。如果客户端连接不终止,则无法关闭数据库。

fast:快速关闭数据库,断开客户端的连接,让已有的事务回滚,然后正常关闭数据库。相当于Oracle数据库关闭时的immediate模式。

immediate:立即关闭数据库,相当于数据库进程立即停止,直接退出,下次启动数据库需要进行恢复。相当于Oracle数据库关闭时的abort模式。

PostgreSQL下的immediate关机模式是相当于Oracle中的Abort的关机模式,而Oracle下的immediate关机模式实际上对应的是PostgreSQL下的fast,Oracle DBA尤其要注意这一点。

其中,比较常用的关闭数据库的方法是“fast”方法。

2.2.8 编译安装时的常见问题及解决方法

问题一:./configure时报“error: zlib library not found”错误是怎么回事?报错信息如下:

osdba@ubuntu01:~/src/postgresql-9.2.3$ ./configure

--prefix=/usr/local/pgsql9.2.3 --with-perl --with-python

checking build system type... x86_64-unknown-linux-gnu

....

....

checking for inflate in -lz... no

configure: error: zlib library not found

If you have zlib already installed, see config.log for details on the failure.  It is possible the compiler isn't looking in the proper directory.

Use --without-zlib to disable zlib support.

答:这是因为没有安装zlib开发包,安装后将不再报错。

问题二:已安装了“libreadline6”的包,但./configure时仍报“error: readline library not found”错误是怎么回事?报错信息如下:

osdba@ubuntu01:~/src/postgresql-9.2.3$ ./configure

--prefix=/usr/local/pgsql9.2.3 --with-perl --with-python

checking build system type... x86_64-unknown-linux-gnu

...

...

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.

答:包安装错了,是需要安装开发包,即安装libreadline6-dev这个包,而不是libreadline6这个包。

问题三:在运行./configure命令时报以下警告,是否会导致编译出来的PostgreSQL的功能缺失?警告信息如下:

checking for bison... no

configure: WARNING:

*** Without Bison you will not be able to build PostgreSQL from Git nor

*** change any of the parser definition files.  You can obtain Bison from

*** a GNU mirror site.  (If you are using the official distribution of

*** PostgreSQL then you do not need to worry about this, because the Bison

*** output is pre-generated.)

checking for flex... no

configure: WARNING:

*** Without Flex you will not be able to build PostgreSQL from Git nor

*** change any of the scanner definition files.  You can obtain Flex from

*** a GNU mirror site.  (If you are using the official distribution of

*** PostgreSQL then you do not need to worry about this because the Flex

*** output is pre-generated.)

答:不会影响编译出来的PostgreSQL功能。这个警告的意思是没有bison和flex是无法使用git方式编译的。这里没有使用git,所以没有关系。bison是自动生成语法分析器的程序,flex则是自动生成词法分析器的程序,在PostgreSQL主要用于SQL的词法解析和语法解析。因为在源码包中已经生成了词法解析和语法解析的C源代码,所以没有bison和flex,也能正常编译。当然也可以把bison和flex这两个工具安装上,命令如下:

sudo aptitude install bison flex

问题四:在运行make时报“cannot find -lperl”的错误为什么?报错信息如下:

gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith

-Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard

-fpic -shared -o plperl.so plperl.o SPI.o Util.o -L../../../src/port

-Wl,--as-needed -Wl,-rpath,'/usr/lib/perl/5.14/CORE',--enable-new-dtags 

-fstack-protector -L/usr/local/lib  -L/usr/lib/perl/5.14/CORE -lperl -ldl -lm

-lpthread -lc -lcrypt

/usr/bin/ld: cannot find -lperl

collect2: error: ld returned 1 exit status

make[3]: *** [plperl.so] Error 1

make[3]: Leaving directory `/home/osdba/src/postgresql-9.2.3/src/pl/plperl'

make[2]: *** [all-plperl-recurse] Error 2

make[2]: Leaving directory `/home/osdba/src/postgresql-9.2.3/src/pl'

make[1]: *** [all-pl-recurse] Error 2

make[1]: Leaving directory `/home/osdba/src/postgresql-9.2.3/src'

make: *** [all-src-recurse] Error 2

答:这是在./configure时加了--with-perl,却没有安装perl开发包导致的。注意,若没有安装perl开发包,在运行./configure时并不报错,而是到运行make命令的时候才报错。在Debian或Ubuntu下,只要安装libperl-dev包即可:

sudo aptitude install libperl-dev

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
7月前
|
SQL 关系型数据库 编译器
PostgreSQL 16源码安装
PostgreSQL 16源码编译,涵盖gcc、clang、pg jit
292 0
|
关系型数据库 Linux 数据库
|
关系型数据库 数据库 开发工具
PostgreSQL 10.0 源码安装
源码安装 tar zxvf postgresql-10.0.tar.gz mv postgresql-10.0 /usr/local/pgsql cd /usr/local/pgsql/ root用户下安装依赖包 yum -y install gcc gcc-c++ automake auto...
1676 0
|
JavaScript 关系型数据库 Linux
PostgreSQL 11源码安装
PostgreSQL 11源码安装 测试环境:RedHat 7.0 Linux 64bit PostgreSQL 11源码安装包下载地址link 系统环境准备 RedHat 7.0默认会安装相关系统包,还需要增加PostgreSQL编译时必须包,如下: gcc, bison, gcc-c++, r.
7470 0
|
关系型数据库 数据库 PostgreSQL
|
关系型数据库 数据库 PostgreSQL
|
关系型数据库 C语言 PostgreSQL
|
运维 关系型数据库 数据库
PostgreSQL运维实战精讲之“postgresql源码安装”
一、下载地址 wget https://ftp.postgresql.org/pub/source/v9.2.4/postgresql-9.2.4.tar.gz 二、安装: #安装依赖包 yum install -y perl-...
881 0
|
关系型数据库 PostgreSQL 数据库
|
26天前
|
关系型数据库 分布式数据库 数据库
成都晨云信息技术完成阿里云PolarDB数据库产品生态集成认证
近日,成都晨云信息技术有限责任公司(以下简称晨云信息)与阿里云PolarDB PostgreSQL版数据库产品展开产品集成认证。测试结果表明,晨云信息旗下晨云-站群管理系统(V1.0)与阿里云以下产品:开源云原生数据库PolarDB PostgreSQL版(V11),完全满足产品兼容认证要求,兼容性良好,系统运行稳定。