分享最新优惠信息
购买主机更加划算

Linux系统从源代码安装PostgreSQL数据库简单方法

 

PostgreSQL全球开发组于2023年2月9日发布了Postgres最新版本15.2。新的PostgreSQL版本的特点如下:

  • Logical Replication:此功能支持跨备用服务器复制单个数据库对象(无论是行、表还是选择性数据库),它提供了对数据复制的更多控制。
  • Quorum Commit for Synchronous Replication:在这个特性中,dba可以指定确认数据库更改已经完成的备用数据库的数量,这样数据就可以被认为是安全写入的。
  • SCRAM-SHA-256身份验证:提高了现有基于MD5的密码身份验证和存储的安全性。
  • 改进了并行查询执行。
  • 声明性表分区。
  • 对JSON和JSONB的全文搜索支持。

在本文中,小编主要介绍如何在Linux系统中使用源代码安装来安装PostgreSQL 15,对于那些希望从分发包管理器轻松安装的朋友可以按照以下指南进行操作。

由于Postgres是一个开源数据库,因此可以根据需要/要求从源代码构建它。另外,还可以通过为各种附加功能提供一个或多个命令行选项来自定义构建和安装过程。使用源代码安装的主要优点是它可以在安装过程中进行高度定制。

从源代码安装PostgreSQL

1、首先使用包管理器安装所需的先决条件,例如gcc、readline-devel和zlib-devel,命令如下:

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

2、直接在系统上使用以下wget命令从postgres官方网站下载源代码tar文件,命令如下:

# wget https://ftp.postgresql.org/pub/source/v15.2/postgresql-15.2.tar.bz2

3、使用tar命令解压下载的tarball文件,这样将创建一个名为postgresql-15.2的新目录:

# tar -xvf postgresql-15.2.tar.bz2
# cd postgresql-15.2
#ls-l

示例输出如下:

total 780
-rw-r--r--  1 1107 1107    397 Feb  6 16:39 aclocal.m4
drwxrwxrwx  2 1107 1107   4096 Feb  6 16:50 config
-rwxr-xr-x  1 1107 1107 601519 Feb  6 16:39 configure
-rw-r--r--  1 1107 1107  89258 Feb  6 16:39 configure.ac
drwxrwxrwx 61 1107 1107   4096 Feb  6 16:50 contrib
-rw-r--r--  1 1107 1107   1192 Feb  6 16:39 COPYRIGHT
drwxrwxrwx  3 1107 1107     87 Feb  6 16:50 doc
-rw-r--r--  1 1107 1107   4264 Feb  6 16:39 GNUmakefile.in
-rw-r--r--  1 1107 1107    277 Feb  6 16:39 HISTORY
-rw-r--r--  1 1107 1107  63842 Feb  6 16:51 INSTALL
-rw-r--r--  1 1107 1107   1875 Feb  6 16:39 Makefile
-rw-r--r--  1 1107 1107   1213 Feb  6 16:39 README
drwxrwxrwx 16 1107 1107   4096 Feb  6 16:51 src

4、安装过程的下一步是通过根据你的需要选择选项来配置下载的源代码。使用./configure --help获取有关各种选项的帮助。

# ./configure --help

`configure' configures PostgreSQL 15.2 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、现在创建一个要安装Postgres文件的目录,并在配置中使用前缀选项。

# mkdir /opt/PostgreSQL
# ./configure --prefix=/opt/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 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 for gcc option to accept ISO C99... none needed
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking for gawk... gawk
checking whether gcc supports -Wdeclaration-after-statement, for CFLAGS... yes
checking whether gcc supports -Werror=vla, for CFLAGS... yes
checking whether gcc supports -Werror=unguarded-availability-new, for CFLAGS... no
....

从源代码构建PostgreSQL

6、配置完成后,接下来将使用以下make命令开始构建postgreSQL,命令如下:

# make

构建过程完成后,现在使用以下命令安装Postgresql:

# make install

这样Postgresql 15已安装在/opt/PostgreSQL目录中。

创建Postgres用户

7、现在创建一个Postgres用户和目录,作为初始化数据库集群的数据目录这个数据目录的所有者应该是一个Postgres用户并且权限应该是700并且还为我们的方便设置了Postgresql二进制文件的路径:

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

初始化Postgres数据库

8、现在,在使用任何Postgres命令之前,以Postgres用户身份使用以下命令初始化数据库。

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

这个-D就是数据库集群的位置在哪里,或者可以说它是我们要初始化数据库集群的数据目录,-U数据库超级用户名,-W是db超级用户的密码提示:

初始化Postgres数据库

有关更多信息和选项,可以参考initdb --help

9、初始化数据库后,启动数据库集群,或者如果需要更改服务器的端口或监听地址,编辑数据库服务器数据目录下的/pgdatabase/data/postgresql.conf文件

配置PostgreSQL端口

$ pg_ctl -D /pgdatabase/data/ start

启动Postgres数据库

10、启动数据库后,使用以下ps和netstat命令验证Postgres服务器进程状态。

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

验证PostgreSQL数据库

可以看到数据库集群运行良好,启动数据库集群时,可以在使用-l选项指定的位置找到启动日志。

11、现在连接到数据库集群并使用以下命令创建数据库:

$ psql -p 51751
postgres=# create database test;
postgres=# \l to list all databases in cluster
postgres=# \q to quit form postgres console

连接PostgreSQL数据库

以上就是Linux系统中从源代码安装PostgreSQL,傻瓜式教程,相对来说比较简单,有不清楚的小伙伴可以参考上述教程进行设置。众所周知,在Linux上从源代码安装PostgreSQL的具体以下主要优势:

  1. 可以使用最新版本:在下载源代码的同时,可以选择要安装的最新版本。这意味着可以使用最新的PostgreSQL功能和改进。
  2. 更好的灵活性:通过从源代码构建,可以更好地控制PostgreSQL安装的细节,例如编译器选项和编译时间选项。这使得你可以根据自己的需要进行优化。
  3. 更高的性能:从源代码安装PostgreSQL可以比使用预编译的二进制包更好地优化数据库服务器,因为可以选择要启用的特定功能,以及进行更细粒度的性能优化。
  4. 更好的定制化:通过从源代码构建,可以使用自己喜欢的配置选项,定制您的PostgreSQL构建,以满足您的具体需求。
  5. 更好的理解:通过从源代码安装PostgreSQL,可以更好地了解PostgreSQL并学习如何编译和构建PostgreSQL源代码。这将使你更好地理解PostgreSQL并学习如何优化它。
未经允许不得转载:惠主机 » Linux系统从源代码安装PostgreSQL数据库简单方法

相关文章