欢迎,来自IP地址为:100.26.35.111 的朋友



PostgreSQL也被称作Postgres,是一款强大的开源关系型数据库系统。系统面向企业级的设计理念,令其具有诸多特性,例如错误日志预写入、异步复制、多版本并发控制(MVCC)、在线热备份、时间点恢复、查询计划/优化、表空间、嵌套事务处理等。有如此多的特性,令PostgreSQL在开源数据库系统领域可以和MySQL展开竞争,并在某些特定应用领域逐渐取代MySQL成为主流。

Postgres于2017年10月5日发布其最新版PostgreSQL 10,新版本PostgreSQL有如下特性:

  • 逻辑复制:此功能允许在备用服务器上复制单个数据库对象(无论是行、表还是选择性数据库)。它提供了对数据复制的更多控制
  • 同步复制的仲裁提交:在这个特性中,DBA现在可以指定确认数据库更改所需的备用号码,这样数据可以被认为是安全写入的
  • SCRAM-SHA-256认证:对现有的基于MD5的密码认证进行安全性的改善
  • 改进的并行查询执行
  • 声明表分区
  • JSON和JSONB全文搜索支持

本文将详细介绍如何在Linux系统中使用源代码安装PostgreSQL 10,为各位Linux用户提供参考。

源代码安装PostgreSQL

由于Postgres是开源数据库,它可以根据每个用户的需求自行编译源代码,可以在编译时使用一个或多个命令行选项来达到特定功能个性编译及安装的目的。

使用源代码安装软件最主要的优势就在于其满足不同需求的个性化。

1、首先使用包管理工具安装一些必要的编译工具,如gcc,、readline-devel以及zlib-devel

# yum install gcc zlib-devel readline-devel     [On RHEL/CentOS]
# apt install gcc zlib1g-dev libreadline6-dev   [On Debian/Ubuntu]

2、使用wget命令从PostgreSQL的官方网站下载源代码压缩包

# wget https://ftp.postgresql.org/pub/source/v10.0/postgresql-10.0.tar.gz

3、使用tar命令解压缩源代码包

# tar -zxvf postgresql-10.0.tar.gz
# ll

典型输出如下:

total 25236
drwxrwxrwx 6 1107 1107     4096 Oct  2 21:15 postgresql-10.0
-rw-r--r-- 1 root root 25830653 Oct  2 21:15 postgresql-10.0.tar.gz

4、下一步就是根据自己的需求来设定源代码编译选项

# # cd postgresql-10.0

使用./configure – -help命令来查看不同选项的帮助信息,示例输出如下:

`configure' configures PostgreSQL 10.0 to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print `checking ...' messages
      --cache-file=FILE   cache test results in FILE [disabled]
  -C, --config-cache      alias for `--cache-file=config.cache'
  -n, --no-create         do not create output files
      --srcdir=DIR        find the sources in DIR [configure dir or `..']

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local/pgsql]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]
......

5、现在新建一个存储PostgreSQL程序文件的目录,并在configure命令中使用prefix选项

# mkdir /opt/PostgreSQL-10/
# ./configure --prefix=/opt/PostgreSQL-10

示例输出如下:

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 WAL segment size... 16MB
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc supports -Wdeclaration-after-statement... yes
checking whether gcc supports -Wendif-labels... yes
checking whether gcc supports -Wmissing-format-attribute... yes
checking whether gcc supports -Wformat-security... yes
checking whether gcc supports -fno-strict-aliasing... yes
checking whether gcc supports -fwrapv... yes
checking whether gcc supports -fexcess-precision=standard... no
....

6、编译完成后,就可以用make命令来生成PostgreSQL

# make

生成完成后,使用以下命令来进行PostgreSQL的安装

# make install

全部执行完成后,Postgresql 10 就会被安装在 /opt/PostgreSQL-10目录。

7、现在需要添加一个postgres用户并创建一个存放数据文件的目录,这个目录的所有者应该为数据库用户postgres,并且将该目录的权限设置为700以保证数据文件的安全性。最后,需要将PostgreSQL的执行文件添加至系统的PATH环境变量中

# useradd postgres
# passwd postgres
# mkdir /pgdatabase/data
# chown -R postgres. /pgdatabase/data
# echo 'export PATH=$PATH:/opt/PostgreSQL-10/bin' > /etc/profile.d/postgres.sh

8、然后就可以切换至postgres用户来对数据库进行初始化操作了

# su postgres
$ initdb -D /pgdatabase/data/ -U postgres -W

其中-D参数用于指定数据文件位置;-U参数用于指定操作数据库的超级用户;-W参数为需要设置超级用户密码。如果想要了解更多initdb工具的详细信息,可以使用initdb –help命令来获得更多帮助信息。

9、数据库初始化完成后,可以通过修改数据文件目录的postgresql.conf配置文件来达到设置postgresql侦听端口的目的

修改完配置文件后,需要用以下命令重新启动PostgreSQL

$ pg_ctl -D /pgdatabase/data/ -l /pglog/db_logs/start.log start

其中-l /pglog/db_logs/start.log参数是用于设置日志文件,方便查看信息,可以根据自己的实际情况自行设置,如不需要也可以去掉这部分内容,并不影响数据库服务器启动。

10、数据库服务启动后,可以用以下命令查看进程信息以及端口信息来进行确认

$ ps -ef |grep -i postgres
$ netstat -apn |grep -i 5432

当看到以上内容时,说明数据库系统已经运行正常。

11、现在,就可以使用如下命令连接数据库,执行SQL语句了

$ psql -p 5432
postgres=# create database test;
postgres=# \l 用于显示所有数据库
postgres=# \q 退出psotgre命令提示符

结果示例如下:

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注