更多"C/C++、PostgreSQL、编译原理、计算机原理、TCP/IP、数据结构&算法、Linux编程”等技术文章更新于公众号: 君子黎
1、PostgreSQL概述
PostgreSQL是一种先进的、流行的对象-关系型数据库管理系统(Object-relational database management system,简称 ORDBMS )。它几乎可以完美的运行于各种平台,且源码开发。它是目前功能最为强大、特性最为丰富、结构最为复杂的开源数据库系统。之所以称其为 ==对象关系型数据库管理系统==,是因为它不但支持关系型数据库的所有功能,而且还具备着类、继承等对象数据库特征。 它具有非常丰富的功能集,有着完善的SQL标准支持、多版本并发控制、时间点恢复、异步复制、嵌套事务、在线/热备份、预写日志容错等技术等等,并且其软件版本非常之稳定。
2、PostgreSQL发展历史
PostgreSQL数据库诞生于加州大学伯克利分校(UCB), 最初是由该大学的 Michael Stonebraker 教授创建,最初他领导了关系型数据Ingres的研究项目计划,而该数据库也是PostgreSQL的雏形。从Ingres到最终我们得以看到的PostgreSQL数据库,中间历经了许多故事,详细细节可以参考Wiki。从PostgreSQL最初的雏形到现在的12版本,其整体发展历程如图1所示。
图1 PostgreSQL数据库发展历程
若查看每个主版本中的每个次版本的发布时间等详细信息,请[查看PostgreSQL官网的版本目录] 。
3、PostgreSQL安装准备
安装设备为CentOS操作系统。该设备硬件各参数指标如下:
操作系统:CentOS Linux release 7.5.1804 (Core)
CPU型号: Intel(R) Xeon(R) CPU E3-1225 v3 @ 3.20GHz
CPU核数: cpu cores : 4
CPU个数: 1
设备内存:MemTotal: 8097544 kB
3.1 源码下载
PostgreSQL官方提供了二进制包和源码编译安装这两种方式。其下载地址是[ PostgreSQL核心发行版]。下载页面如下图2所示。
图2 PostgreSQL二进制包及源码包下载地址
本次采用源码编译安装方式,==点击== 源代码 下面的 文件浏览器,将会自动跳转到PostgreSQL数据库所有的版本列表页面。然后选择自己需要的版本(==本次测试的PostgreSQL版本是11.5==)。==如图3所示==。
图3 PostgreSQL源码包列表下载地址
3.2 源码编译安装
新建一个目录,用于存放从PostgreSQL官网下载来的 xxx.tar.gz 格式的压缩包。
3.2.1 新建 TestPgSql115 目录,用于存放postgresql-11.5.tar.gz压缩包
3.2.2 拷贝压缩包,并解压
将 postgresql-11.5.tar.gz 拷贝至该目录下。并解压该文件(tar -zxvf postgresql-11.5.tar.gz)====, 得到==postgresql-11.5==。
3.2.3 cd 到解压出来的postgresql-11.5目录
3.2.4 开始配置(./configure)、编译(make)并安装(make install)
源码编译安装 共有3部曲,依次是:./configure(配置) 、 make(编译) 、 make install(安装)。==configure==是一个配置文件脚本,用于配置软件包。用来检测你的安装平台的一些目标特征。比如它会检测你当前的系统环境是不是有CC或GCC,注意,它只是检测,并不是说它需要这些CC/GCC(真正生产可执行文件和相关库文件是在make install安装之后)。执行该脚本时候,如果不配置--prefix该选项,则安装后可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr /local/share,比较凌乱且不易管理。每个软件包的安装,其==configure==脚本所支持的选项参数也不相同,但是对应的该软件包一般都会附加README.md文件进行说明的。 ./configure --help 可以看到,当前的configure配置脚本所支持的所有参数选项。如图所示:
选项太多,该图只截取一小部分数据。上图中红色框 处说明,如果不指定安装路径,则PostgreSQL 将会被安装到 /usr/local/pgsql 目录下。
3.2.4.1 ./configure
这里指定安装后的可执行文件资源放在/home/ssd/TestPgSql115/PG_DATA(./configure --prefix=/home/ssd/TestPgSql115/PG_DATA)目录下。
3.2.4.2 make
执行make命令,如下图所示。
3.2.4.3 make install
该命令执行大概需要6分钟左右,具体时间数组跟你当前的设备硬件配置有关系。配置越高,性能越好,则编译并安装的速度也越快,反之,则需要更久的时间。安装结束之后,终端会打印提示 PostgreSQL已安装完成。
3.2.5 配置环境变量
PostgreSQL安装完成之后,为了操作的方便,最好将其路径加入到==环境变量配置文件==/etc/profile 。这样之后的所有系列操作,都不需要加上命令的绝对路径而操作,会方便许多。
LD_LIBRARY_PATH路径为PostgreSQL数据库源码编译出来之后的动态库所存放位置。
libecpg.a libecpg_compat.so.3 libecpg.so.6 libpgfeutils.a libpgtypes.so libpq.a(静态库) libpq.so.5.11 libecpg_compat.a libecpg_compat.so.3.11 libecpg.so.6.11 libpgport.a libpgtypes.so.3 libpq.so(动态库) pkgconfig libecpg_compat.so libecpg.so libpgcommon.a libpgtypes.a libpgtypes.so.3.11 libpq.so.5 postgresql
修改之后,执行 source /etc/profile 使其生效。
3.2.6 查看psql版本(psql --version)
4、初始化PostgreSQL数据库集簇
在上一级中3的一系列操作下,PostgreSQL数据库已经安装完成。然而,这并不表示所有的工作均已做完。 因为我们还未指定PostgreSQL数据库表的数据存放位置,还未生成模板数据库以及相应的文件等信息。所以这个时候PostgreSQL是还不能正常工作的。必须等指定说明PostgreSQL数据库中数据表中数据存放位置等等之后才可以工作。
4.1 创建数据库集簇目录
创建data目录(绝对路径:/home/ssd/TestPgSql115/data)用来存储PostgreSQL数据库表中的数据和配置文件等信息。并修改其目录的操作权限chown postgres:postgres /home/ssd/TestPgSql115/data/
[root@Thor TestPgSql115]# ll 总用量 25388 drwx------ 2 postgres postgres 4096 10月 23 20:44 data drwxr-xr-x 6 root root 4096 10月 21 15:41 PG_DATA drwxrwxrwx 6 1107 1107 4096 10月 23 18:07 postgresql-11.5 -rw-r--r-- 1 root root 25940612 10月 21 15:30 postgresql-11.5.tar.gz -rw-r--r-- 1 root root 122 10月 21 19:22 start.sh -rw-r--r-- 1 root root 113 10月 21 19:22 stop.sh
4.2 创建数据库集簇
创建命令:su postgres -c '/home/ssd/TestPgSql115/PG_DATA/bin/initdb -D /home/ssd/TestPgSql115/data/'
提示数据库集簇已经创建成功,并且现在你可以使用/home/ssd/TestPgSql115/PG_DATA/bin/pg_ctl -D /home/ssd/TestPgSql115/data/ -l logfile start
命令来启动数据库的进程服务。此刻,该data目录下多了这些目录和文件。
drwx------ 5 postgres postgres 4.0K 10月 23 20:51 base drwx------ 2 postgres postgres 4.0K 10月 23 20:51 global drwx------ 2 postgres postgres 4.0K 10月 23 20:51 pg_commit_ts drwx------ 2 postgres postgres 4.0K 10月 23 20:51 pg_dynshmem -rw------- 1 postgres postgres 4.5K 10月 23 20:51 pg_hba.conf -rw------- 1 postgres postgres 1.6K 10月 23 20:51 pg_ident.conf drwx------ 4 postgres postgres 4.0K 10月 23 20:51 pg_logical drwx------ 4 postgres postgres 4.0K 10月 23 20:51 pg_multixact drwx------ 2 postgres postgres 4.0K 10月 23 20:51 pg_notify drwx------ 2 postgres postgres 4.0K 10月 23 20:51 pg_replslot drwx------ 2 postgres postgres 4.0K 10月 23 20:51 pg_serial drwx------ 2 postgres postgres 4.0K 10月 23 20:51 pg_snapshots drwx------ 2 postgres postgres 4.0K 10月 23 20:51 pg_stat drwx------ 2 postgres postgres 4.0K 10月 23 20:51 pg_stat_tmp drwx------ 2 postgres postgres 4.0K 10月 23 20:51 pg_subtrans drwx------ 2 postgres postgres 4.0K 10月 23 20:51 pg_tblspc drwx------ 2 postgres postgres 4.0K 10月 23 20:51 pg_twophase -rw------- 1 postgres postgres 3 10月 23 20:51 PG_VERSION drwx------ 3 postgres postgres 4.0K 10月 23 20:51 pg_wal drwx------ 2 postgres postgres 4.0K 10月 23 20:51 pg_xact -rw------- 1 postgres postgres 88 10月 23 20:51 postgresql.auto.conf -rw------- 1 postgres postgres 24K 10月 23 20:51 postgresql.conf
5、启动PostgreSQL数据库服务器
开始启动PostgreSQL数据库进程(修改postgresql.conf配置文件中端口为5678)。命令:su postgres -c '/home/ssd/TestPgSql115/PG_DATA/bin/pg_ctl -D /home/ssd/TestPgSql115/data/ -m fast'
(1) chmod 700 -R /home/ssd/TestPgSql115/data/
(2) su postgres -c '/home/ssd/TestPgSql115/PG_DATA/bin/pg_ctl -D /home/ssd/TestPgSql115/data/ -m fast start'
****
**** 2021-02-23 15:43:01补充 ***
备注:若目录权限不正确,则postgreSQL服务启动会失败,并报错响应提示,如下:
[root@Thor bin]# su postgres -c '/usr/local/postgresql13_2/bin/pg_ctl -D /home/ssd/psql13_2/ -m fast start' waiting for server to start....2021-02-23 15:35:36.238 CST [3847] FATAL: data directory "/home/ssd/psql13_2" has invalid permissions 2021-02-23 15:35:36.238 CST [3847] DETAIL: Permissions should be u=rwx (0700) or u=rwx,g=rx (0750). stopped waiting pg_ctl: could not start server Examine the log output.
对于数据库蔟的存储目录(/home/ssd/psql13_2/)权限详情如下:
drwxrwxrwx 19 postgres postgres 4096 Feb 23 15:26 psql13_2
现修改其目录用户权限为700(chmod 700 /home/ssd/psql13_2/),则为:
drwx------ 19 postgres postgres 4.0K Feb 23 15:36 psql13_2
重新使用上面的命令启动数据库进程,则可以看到成功的提示信息。
[root@Thor bin]# su postgres -c '/usr/local/postgresql13_2/bin/pg_ctl -D /home/ssd/psql13_2/ -m fast start' waiting for server to start....2021-02-23 15:36:02.394 CST [7802] LOG: starting PostgreSQL 13.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit 2021-02-23 15:36:02.395 CST [7802] LOG: listening on IPv6 address "::1", port 5566 2021-02-23 15:36:02.395 CST [7802] LOG: listening on IPv4 address "127.0.0.1", port 5566 2021-02-23 15:36:02.418 CST [7802] LOG: listening on Unix socket "/tmp/.s.PGSQL.5566" 2021-02-23 15:36:02.442 CST [7994] LOG: database system was shut down at 2021-02-23 15:24:41 CST 2021-02-23 15:36:02.454 CST [7802] LOG: database system is ready to accept connections done server started
****
提示PostgreSQL数据库进程服务已经起来了。查看下5678端口是否已经在监听。
6、登录数据库Test
使用命令psql -p 5678b -U postgres;
登录PostgreSQL数据库(若想要登录时候输入密码,则加上 -W),其中==p表示端口==, ==U表示登录用户==。可以看到已经成功登录数据库。
查看PostgreSQL数据库服务器的版本,看是否未为11.5。