Postgresql基于Linux各种安装方法

说明

目前PostGreSQL最新版本为11.3,9.3及之前版本以不再受官方支持.从RHEL/CentOS/SL/OL 6开始都自带PG数据库,下表是不同的操作系统版本对应的数据库版本:

操作系统版本自带数据库版本
RHEL 810 and 9.6 via modules
RHEL/CentOS/SL/OL 79.2 (also supplies package rh-postgresql10, rh-postgresql96,
RHEL/CentOS/SL/OL 68.4 (also supplies package rh-postgresql96, via SCL)
Fedora 3011
Fedora 2910

安装方法 - Linux

PG安装方法很多,和MySQL类似,给用户提供很大的选择空间。如:RPM包安装(在线、离线)、源码编译安装、系统自带、二进制、NDB安装等。下面一个一个介绍:

使用系统自带的PG

如果以上自带版本满足了你的需求,那么就可以直接使用而无需再次安装,但是默认是没有启用自动启动和没有初始化,所以需要初始化并启用开机自启动。

  • RHEL / CentOS / SL / OL 6
    service postgresql initdb
    chkconfig postgresql on
    
  • RHEL / CentOS / SL / OL 7, 8 Or Fedora 29
    postgresql-setup initdb
    systemctl enable postgresql.service
    systemctl start postgresql.service
    

具体例子:

  • 初始化数据库
    [root@localhost ~]# postgresql-setup initdb
    Initializing database ... OK
    
  • 启用开机自动
    [root@localhost ~]# systemctl enable postgresql.service
    Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql.service to /usr/lib/systemd/system/postgresql.service.
    
  • 打开数据库
    [root@localhost ~]# systemctl start postgresql.service
    
  • 连接数据库(postgres用户连接)
      [root@localhost ~]# su - postgres
      -bash-4.2$ psql
      psql (9.2.23)
      Type "help" for help.
    
      postgres=# select version();
                                                      version
      ---------------------------------------------------------------------------------------------------------------
      PostgreSQL 9.2.23 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
      (1 row)
    

如果想要使用root用户连接,则需要先创建用户才行,否则会报错:

[root@localhost ~]# psql
psql: FATAL:  role "root" does not exist

解决方法:
使用postgres用户连接数据库,然后创建root用户即可,如下:

[root@~]#su - postgres
Last login: Wed Apr  1 10:28:10 CST 2015 on pts/2
-bash-4.2$ psql -U postgres
psql (9.4.0)
Type "help" for help.

postgres=#create user root with password 'password';
CREATE ROLE
postgres=# GRANT ALL PRIVILEGES ON DATABASE mydatabase to root;
GRANT
postgres=# ALTER ROLE root WITH SUPERUSER;

postgres=# \q

在线RPM包安装

首先卸载掉系统自带的PG数据库,再次安装新版本的PG。

卸载自带PG

[root@localhost ~]# rpm -qa|grep postgre
postgresql-libs-9.2.23-3.el7_4.x86_64
postgresql-server-9.2.23-3.el7_4.x86_64
postgresql-9.2.23-3.el7_4.x86_64
postgresql-docs-9.2.23-3.el7_4.x86_64

--要安装以下顺序依次卸载,否则会报依赖错误
[root@localhost ~]# rpm -e postgresql-server-9.2.23-3.el7_4.x86_64
[root@localhost ~]# rpm -e postgresql-docs-9.2.23-3.el7_4.x86_64
[root@localhost ~]# rpm -e postgresql-9.2.23-3.el7_4.x86_64
[root@localhost ~]# rpm -e postgresql-libs-9.2.23-3.el7_4.x86_64

安装、配置YUM源

yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

安装PG

查看当前YUM源的PG 11版本的安装包

[root@localhost ~]# yum search postgresql11
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
======================================================================= N/S matched: postgresql11 ========================================================================
postgresql11-debuginfo.x86_64 : Debug information for package postgresql11
postgresql11.x86_64 : PostgreSQL client programs and libraries
postgresql11-contrib.x86_64 : Contributed source and binaries distributed with PostgreSQL
postgresql11-devel.x86_64 : PostgreSQL development header files and libraries
postgresql11-docs.x86_64 : Extra documentation for PostgreSQL
postgresql11-libs.x86_64 : The shared libraries required for any PostgreSQL clients
postgresql11-llvmjit.x86_64 : Just-in-time compilation support for PostgreSQL
postgresql11-odbc.x86_64 : PostgreSQL ODBC driver
postgresql11-plperl.x86_64 : The Perl procedural language for PostgreSQL
postgresql11-plpython.x86_64 : The Python procedural language for PostgreSQL
postgresql11-pltcl.x86_64 : The Tcl procedural language for PostgreSQL
postgresql11-server.x86_64 : The programs needed to create and run a PostgreSQL server
postgresql11-tcl.x86_64 : A Tcl client library for PostgreSQL
postgresql11-test.x86_64 : The test suite distributed with PostgreSQL

安装客户端和server

[root@pg ~]# yum install postgresql11 postgresql11-server
省略。。。
Running transaction
  Installing : postgresql11-libs-11.3-1PGDG.rhel7.x86_64                                                                                                              1/4
  Installing : libicu-50.1.2-15.el7.x86_64                                                                                                                            2/4
  Installing : postgresql11-11.3-1PGDG.rhel7.x86_64                                                                                                                   3/4
  Installing : postgresql11-server-11.3-1PGDG.rhel7.x86_64                                                                                                            4/4
lei/productid                                                                                                                                      | 1.6 kB  00:00:00
  Verifying  : postgresql11-11.3-1PGDG.rhel7.x86_64                                                                                                                   1/4
  Verifying  : libicu-50.1.2-15.el7.x86_64                                                                                                                            2/4
  Verifying  : postgresql11-server-11.3-1PGDG.rhel7.x86_64                                                                                                            3/4
  Verifying  : postgresql11-libs-11.3-1PGDG.rhel7.x86_64                                                                                                              4/4

Installed:
  postgresql11.x86_64 0:11.3-1PGDG.rhel7                                           postgresql11-server.x86_64 0:11.3-1PGDG.rhel7

Dependency Installed:
  libicu.x86_64 0:50.1.2-15.el7                                                postgresql11-libs.x86_64 0:11.3-1PGDG.rhel7

Complete!

初始化数据库并设置开机自启动

  • 初始化数据库
    [root@pg ~]# /usr/pgsql-11/bin/postgresql-11-setup initdb
    Initializing database ... OK
    
  • 设置开机自启动
    [root@pg ~]# systemctl enable postgresql-11
    Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-11.service to /usr/lib/systemd/system/postgresql-11.service.
    
  • 打开数据库
    [root@pg ~]#systemctl start postgresql-11
    

离线RPM包安装

和在线安装方法一样,只不过需要提前把RPM包下载下来上传到服务器上进行安装即可。
下载地址:https://yum.postgresql.org/rpmchart.php
根据自己的操作系统版本,下载对应的数据库版本安装即可。
只需要下载下面三个包即可:

postgresql11-libs-11.2-1PGDG.rhel7.x86_64.rpm
postgresql11-server-11.2-1PGDG.rhel7.x86_64.rpm
postgresql11-11.2-1PGDG.rhel7.x86_64.rpm

注:先安装lib包。

NDB安装[不推荐]

从PG 11版本开始,不再支持Linux“一键”安装包NDB了,PG所有安装包下载地址:下载地址如下:

PostgreSQL VersionLinux x86-64Linux x86-32Mac OS XWindows x86-64Windows x86-32
11.3N/AN/ADownloadDownloadN/A
10.8DownloadDownloadDownloadDownloadDownload
9.6.13DownloadDownloadDownloadDownloadDownload
9.5.17DownloadDownloadDownloadDownloadDownload
9.4.22DownloadDownloadDownloadDownloadDownload
9.3.25 (Not Supported)DownloadDownloadDownloadDownloadDownload

下载NDB安装包

wget https://get.enterprisedb.com/postgresql/postgresql-10.8-1-linux-x64.run

安装

一键安装,其实也不方便,需要指定很多信息,比如:软件安装目录,数据文件存放目录,端口,超级用户密码、其他工具安装等等。

  • 赋可执行权限
    [root@pg ~]# chmod +x postgresql-10.8-1-linux-x64.run
    
  • 安装
[root@pg ~]# ./postgresql-10.8-1-linux-x64.run
----------------------------------------------------------------------------
Welcome to the PostgreSQL Setup Wizard.

----------------------------------------------------------------------------
Please specify the directory where PostgreSQL will be installed.

Installation Directory [/opt/PostgreSQL/10]:   --软件安装目录

----------------------------------------------------------------------------
Select the components you want to install; clear the components you do not want
to install. Click Next when you are ready to continue.

PostgreSQL Server [Y/n] :Y

pgAdmin 4 [Y/n] :Y    --监控工具[可选]

Stack Builder [Y/n] :Y   --[可选]

Command Line Tools [Y/n] :Y  --命令行工具,需要安装

Is the selection above correct? [Y/n]: Y   --确认配置

----------------------------------------------------------------------------
Please select a directory under which to store your data.

Data Directory [/opt/PostgreSQL/10/data]:   --数据文件存放目录

----------------------------------------------------------------------------
Please provide a password for the database superuser (postgres). A locked Unix
user account (postgres) will be created if not present.

Password :
Retype password :
----------------------------------------------------------------------------
Please select the port number the server should listen on.

Port [5432]:

----------------------------------------------------------------------------
Advanced Options

Select the locale to be used by the new database cluster.

Locale

[1] [Default locale]

省略。。。
[768] zh_SG.utf8
[769] zh_TW.euctw
[770] zh_TW.utf8
[771] zu_ZA
[772] zu_ZA.iso88591
[773] zu_ZA.utf8
Please choose an option [1] :

----------------------------------------------------------------------------
Pre Installation Summary

The following settings will be used for the installation::

Installation Directory: /opt/PostgreSQL/10
Server Installation Directory: /opt/PostgreSQL/10
Data Directory: /opt/PostgreSQL/10/data
Database Port: 5432
Database Superuser: postgres
Operating System Account: postgres
Database Service: postgresql-10
Command Line Tools Installation Directory: /opt/PostgreSQL/10
pgAdmin4 Installation Directory: /opt/PostgreSQL/10/pgAdmin 4
Stack Builder Installation Directory: /opt/PostgreSQL/10

Press [Enter] to continue:

----------------------------------------------------------------------------
Setup is now ready to begin installing PostgreSQL on your computer.

Do you want to continue? [Y/n]: y

----------------------------------------------------------------------------
Please wait while Setup installs PostgreSQL on your computer.

 Installing
 0% ______________ 50% ______________ 100%
 #########################################

----------------------------------------------------------------------------
Setup has finished installing PostgreSQL on your computer.

安装成功!

进入数据库

安装后数据库自动启动

[root@pg bin]# su - postgres
Last login: Tue May 28 01:56:52 EDT 2019
-bash-4.2$ psql
-bash: psql: command not found
-bash-4.2$ cd /opt/PostgreSQL/10/bin/
-bash-4.2$ pwd
/opt/PostgreSQL/10/bin
-bash-4.2$ ./psql
Password:
psql.bin (10.8)
Type "help" for help.

postgres=# select version();
                                                 version
---------------------------------------------------------------------------------------------------------
 PostgreSQL 10.8 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23), 64-bit
(1 row)

更多信息,参考:https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

源码编译安装

源码安装需要自己编译成安装包,然后再安装。好处是可以根据自己的需要,修改相应的配置,如:数据文件存放目录、默认端口、字符集、软件安装目录等。

要求

默认情况下,以下要求必须满足。否则无法使用某些特性,具体说明如下:

  • GUN make版本需3.8及以上版本
      [root@pg ~]# make --version
      GNU Make 3.82
      Built for x86_64-redhat-linux-gnu
      Copyright (C) 2010  Free Software Foundation, Inc.
      License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
      This is free software: you are free to change and redistribute it.
      There is NO WARRANTY, to the extent permitted by law.
    
  • 需安装GCC包
  • 需安装tar
    用于解压缩文件
  • 默认需要GNU Readline library
    其作用是可以让psql命令行记住执行过的命令,并且可以通过键盘上下键切换命令。但是可以通过--without-readline禁用这个特性,或者可以指定--withlibedit-preferred选项来使用libedit
  • 默认使用zlib压缩库
    可通过--without-zlib选项来禁用

以下是可选配置选项,如果需要使用到某些特性,那么需要安装特定的包,如下:

  • PL/Perl
    使用Perl编程语言操作PG,需要Perl版本5.8.3及以上版本
  • PL/Python
    使用Python语言操作PG,需要Python2.4及以上版本
  • PL/Tcl
    Tcl 8.4及以上版本
  • 支持加密客户端连接
    需要安装OpenSSL 0.9.8及以上版本

如果你是通过Git源码而不是官方发布的源码包,那么还需要以下包:

  • 需要Flex和Bison来从Git一个分支来编译,Flex 2.5.31以以上和Bison 1.875及以上版本。
  • Perl 5.8.3及以上版本

下载源码包

下载地址:https://www.postgresql.org/ftp/source/v11.3/

[root@pg ~]# ll postgresql-11.3.tar.gz
-rw-r--r--. 1 root root 25868246 May 28 02:09 postgresql-11.3.tar.gz

解压源码包

[root@pg ~]# gunzip postgresql-11.3.tar.gz
[root@pg ~]# tar xf postgresql-11.3.tar

tar zxvf postgresql-11.3.tar.gz

注:如果你下载的是.bz2源码包,则使用bunzip2命令来解压源码包。

配置、检查

使用./configure来配置源码树,如果什么都不指定,则指定./configure命令即可,但是这就没必要使用源码方式来安装PG了,完全可以使用RPM包或二进制包来安装。实际环境中,如果不是特殊情况,推荐RPM包、二进制包来安装。因为源码安装复杂,容易出错,而且需要进行全面的测试才能使用。

编译常用的选项:

  • –prefile=目录
    指定软件安装目录,PG所有文件都会被默认安装到该目录的子目录下。如果不指定该选择,则默认/usr/local/pgsql。
  • –exec-prefix=目录
    在安装目录下指定目录,用于存放与体系结构相关的文件。如果不指定,则使用软件安装目录。
  • –bindir=目录
    存放PG的可执行程序,也就是–exec-prefix=目录/bin目录。
  • –sysconfdir=目录
    指定目录,用于存放各种配置文件
  • –datadir=目录
    指定目录,用于存放安装程序使用的只读数据文件
  • –with-extra-version=STRING
    将STRING追加到PG版本号后面。
  • –with-pgport=端口
    指定PG端口号,默认:5432
  • –with-python
    构建PL/Python服务端语言,需要安装python-devel包
  • –with-perl
    构建PL/perl服务端语言
  • –with-openssl
    启用SSL加密连接,需要安装openssl-devel包
  • –with-systemd
    使用systemd来管理PG启停,需要安装systemd-devel包
  • –without-readline
    禁用psql命令行记录历史命令功能,无特殊情况不需要指定该选项。
  • –with-segsize=SEGSIZE
    设置段大小,以G为单位。大表被分成多个操作系统文件,每个文件的大小都等于段的大小(默认1G)。这避免了存在于许多平台上的文件大小限制问题。默认的段大小为1G,在所有支持的平台上都是安全的。如果您的操作系统支持“largefile”(现在大多数都支持),那么您可以使用更大的段大小。这有助于减少在处理非常大的表时使用的文件描述符的数量。但是要注意,不要选择比您的平台和您打算使用的文件系统所支持的值更大的值。您可能希望使用的其他工具,如tar,也可以设置可用文件大小的限制。建议(虽然不是绝对必需的)这个值是2的幂。注意,更改这个值需要initdb来初始化数据库。
  • –with-blocksize=BLOCKSIZE
    块大小,单位:K。这是存储和I/O的最小单位,默认8 K。不建议修改
  • –with-wal-blocksize=BLOCKSIZE
    WAL块大小,单位KB,是WAL I/O和存储的单元,范围1-64,且必须是2的N次方。
  • –enable-debug
    开启DEBUG模式,用来跟踪问题。生产环境禁用。

配置
以下选择仅仅是为了实验测试,千万不要用于生产环境
安装相关包;

yum install readline-devel gcc zlib-devel openssl-devel systemd-devel

注意:如果没有指定OPEN-SSL则不需要安装openssl-devel

编译:

[root@pg postgresql-11.3]# ./configure --prefix=/usr/local/pgsql11 --with-blocksize=16 --with-segsize=2 --with-wal-blocksize=64 --with-pgport=6543 --with-systemd --with-python --with-openssl
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
省略。。。
config.status: linking src/backend/port/dynloader/linux.h to src/include/dynloader.h
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port

编译

检查通过后,可通过makemake all命令即可完成编译,需要一点时间。如下:

[root@pg postgresql-11.3]# make

如果你想要把所有能够东西都编译进来,那么可以使用make world命令,表示:把世界都编译进来。包括:HTML和man信息。
最后结果:

All of PostgreSQL successfully made. Ready to install.

出现以上信息,表示编译成功。

编译后生成的安装文件:

[root@pg postgresql-11.3]# ll ../
total 249532
-rw-------. 1 root root      1548 Nov 25  2028 anaconda-ks.cfg
-rwxr-xr-x. 1 root root 124788182 May 28 01:50 postgresql-10.8-1-linux-x64.run
drwxrwxrwx. 6 1107 1107      4096 May 28 03:19 postgresql-11.3
-rw-r--r--. 1 root root 130723840 May 28 02:09 postgresql-11.3.tar

安装

[root@pg postgresql-11.3]# make install
make[1]: Leaving directory `/root/postgresql-11.3/config'
PostgreSQL installation complete

安装成功!

如果只安装PG客户端,那么可以只执行以下命令:

make -C src/bin install
make -C src/include install
make -C src/interfaces install
make -C doc install

卸载
可通过make uninstall命令来卸载已安装的数据库,但是不会删除已创建的目录。

清除
安装完成后,可通过make clean命令来清除编译产生的文件,以释放磁盘空间。

查看已经安装的数据库文件

[root@pg postgresql-11.3]# ll /usr/local/pgsql11/
total 16
drwxr-xr-x. 2 root root 4096 May 28 03:31 bin
drwxr-xr-x. 6 root root 4096 May 28 03:31 include
drwxr-xr-x. 4 root root 4096 May 28 03:31 lib
drwxr-xr-x. 6 root root 4096 May 28 03:31 shar

配置环境变量

安装完成后,还需要进行一些环境变量的配置,如LD_LIBARY_PATH和PATH。

  • LD_LIBARY_PATH
    在/etc/profile或~/.bash_profile文件中添加以下内容;
    LD_LIBRARY_PATH=/usr/local/pgsql11/lib
    export LD_LIBRARY_PATH
    
    执行source /etc/profile使其生效.
    也可通过setenv LD_LIBRARY_PATH /usr/local/pgsql11/lib临时配置。
    如果是Linux并且有root用户权限,也可通过以下命令
    /sbin/ldconfig /usr/local/pgsql11/lib/
    
  • PATH
    为了方便使用PG可执行程序,如psql命令。
    在/etc/profile或~/.bash_profile文件中添加以下内容;
    PATH=/usr/local/pgsql11/bin:$PATH
    export PATH
    

初始化数据库

  • root用户创建数据文件目录并赋权限
[root@pg postgresql-11.3]# mkdir /usr/local/pgsql11/data
[root@pg postgresql-11.3]# chown postgres /usr/local/pgsql11/data
  • 使用postgres用户初始化数据库
[root@pg postgresql-11.3]# su - postgres
Last login: Tue May 28 03:44:20 EDT 2019 on pts/0
-bash-4.2$ initdb -D /usr/local/pgsql11/data/
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /usr/local/pgsql11/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /usr/local/pgsql11/data/ -l logfile start

打开数据库

根据上面提示,打开数据库

-bash-4.2$ pg_ctl -D /usr/local/pgsql11/data/ -l logfile start
waiting for server to start.... done
server started

查看blocksize

postgres=# show block_size;
 block_size
------------
 16384
(1 row)

块大小是16k,和编译保持一致。

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在金仓上安装基于Linux的数据库,需按照以下步骤进行操作: 1. 确保系统兼容性:首先要检查金仓服务器所运行的Linux发行版本是否与要安装的数据库兼容。例如,常用的数据库如MySQLPostgreSQL和MongoDB都可以在大多数Linux发行版上安装和运行。 2. 下载数据库软件包:从官方网站或其他可靠来源下载所需的数据库软件包。确保下载版本适用于所使用的Linux发行版。 3. 解压软件包:使用压缩解压工具将下载的软件包解压到指定的目录。请根据官方文档提供的指南进行操作。 4. 创建数据库用户:在金仓上创建一个新的数据库用户,以便将来可以使用该用户登录并管理数据库。 5. 配置数据库:根据数据库的要求,对其进行必要的配置。这包括设置数据库的存储路径、端口号、访问控制和安全设置等。 6. 启动数据库:使用提供的启动脚本或命令启动数据库服务。在启动过程中,可以监控控制台输出以确保数据库成功启动。 7. 连接和验证:使用数据库客户端工具或命令行连接到数据库,并验证数据库的正常运行和可访问性。 8. 安全性设置:根据需求,可以对数据库进行安全性设置,例如设置访问权限、加密传输和创建备份等。 9. 配置自动启动:为了实现数据库的自动启动,可以将启动脚本或服务添加到系统启动项中。 10. 测试和优化:在数据库正常运行后,进行一些基本的测试,以确保其在金仓中的性能和稳定性。根据需要进行性能调优和优化。 请注意,具体的步骤可能因所使用的数据库和Linux发行版而有所不同。因此,建议在安装之前仔细阅读官方文档,并按照官方提供的指南进行操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值