PostgreSQL数据库安装方法总结

1 引言

PostgreSQL数据库现在变得越来越流行,在DB-Engines网站(https://db-engines.com/en/ranking ),排名第四,本人所在的公司也是极力推广PG数据库,开源社区中,PG的活跃度也是非常高,本文简单介绍一下pg数据库的几种安装方法。

2 安装

2.1 部署规划

本文总结了PostgreSQL数据库的三种安装方法,都是基于CentOS7.6版本来进行,可以通过/etc/redhat-release来查看具体的版本。

1[root@pgDatabase ~]# cat /etc/redhat-release 2CentOS Linux release 7.6.1810 (Core)

对于数据库的目录规划,并没有严格的规定,但是生产环境中,还是建议将数据目录放在挂载的盘中,防止由于系统的问题导致数据库无法启动,数据丢失。

目录

存放位置

home目录

/home/postgres

安装目录

/usr/local/

数据目录

/data/pgdata

安装文件介绍和下载链接,默认版本为pg9.6。

安装方式

安装包名称

下载地址

tar.gz文件解压直接安装

postgresql-9.6.13-4-linux-x64-binaries.tar.gz

https://www.enterprisedb.com/download-postgresql-binaries

源码编译安装

postgresql-9.6.10.tar.gz

http://ftp.postgresql.org/pub/source/

Rpm包安装

postgresql-server、postgresql-contrib、postgresql-libs、postgresql

https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/

2.2 解压安装

2.2.1 创建postgres用户

1useradd postgres 2passwd postgres

登陆一下,让系统创建家目录

su - postgres

2.2.2 创建目录

1mkdir -p /data/pgdata 2chown -R postgres:postgres /data/pgdata/ 3chmod -R 775 /data/pgdata/

2.2.3 解压安装

1tar -xzvf postgresql-9.6.13-4-linux-x64-binaries.tar.gz -C /data/ 2chown -R postgres /data/pgsql

解压后的文件夹名字是pgsql

创建软连接

1cd /usr/local 2ln -s /data/pgsql pgsql 3

配置环境变量

1cd /home/postgres 2vi .bash_profile 3# 在path后面添加数据库的bin目录方便启动数据库 4PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/pgsql/bin

2.2.4 初始化数据库

1su - postgres 2initdb -D /data/pgdata/

修改配置文件

1cd /data/pgdata/ 2vi postgresql.conf 3# 将该条配置反注释,将localhost改成* 4listen_addresses = '*'

修改访问控制文件

1vi pg_hba.conf 2# IPv4 local connections: 3host all all 0.0.0.0/0 md5 4

说明:

1# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi", 2# "ident", "peer", "pam", "ldap", "radius" or "cert". Note that 3# "password" sends passwords in clear text; "md5" is preferred since 4# it sends encrypted passwords. 5

关闭防火墙

systemctl stop firewalld

2.2.5 启动数据库

1[postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start 2server starting

进入数据库修改postgres密码

1psql 2postgres=# ALTER USER postgres WITH PASSWORD 'postgres'; 3ALTER ROLE

这样就算全部完成了,远程也可以连接数据库了。

2.3 源码安装

2.3.1 下载源码包

如果服务器可以联网,可以使用wget命令来下载。或者从网站上下载然后传到服务器也可以。

wget http://ftp.postgresql.org/pub/source/v9.6.13/postgresql-9.6.13.tar.gz

2.3.2 安装依赖包

安装前必须要保证gcc编译器已经安装好。可以使用rpm -qa gcc检查。

1yum install gcc make openssl zlib-devel -y 2

2.3.3 解压安装包

tar -xzvf postgresql-9.6.13.tar.gz -C /usr/local

2.3.4 编译并安装

1cd /usr/local/postgresql-9.6.13/ 2./configure --prefix=/usr/local/pgsql

问题

1... 2configure: error: readline library not found 3If you have readline already installed, see config.log for details on the 4failure. It is possible the compiler isn't looking in the proper directory. 5Use --without-readline to disable readline support. 6...

按照提示后面加上即可--without-readline

./configure --prefix=/usr/local/pgsql --without-readline

安装,该过程比较长,一般在5~10分钟左右,安装完后,会在/usr/local目录下看到pgsql文件夹,也就是安装完后的数据库的位置。

make && make install

2.3.5 创建postgres用户

1useradd postgres 2passwd postgres

登陆一下,让系统创建家目录

su - postgres

配置环境变量

1cd /home/postgres 2vi .bash_profile 3# 在path后面添加数据库的bin目录方便启动数据库 4PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/pgsql/bin

2.3.6 创建目录

1mkdir -p /data/pgdata 2chown -R postgres:postgres /data/pgdata/ 3chmod -R 700 /data/pgdata/

2.3.7 初始化数据库

1su - postgres 2initdb -D /data/pgdata/

输出以下内容则表明成功。

1The files belonging to this database system will be owned by user "postgres". 2This user must also own the server process. 3 4The database cluster will be initialized with locale "en_US.UTF-8". 5The default database encoding has accordingly been set to "UTF8". 6The default text search configuration will be set to "english". 7 8Data page checksums are disabled. 9 10fixing permissions on existing directory /data/pgdata ... ok 11creating subdirectories ... ok 12selecting default max_connections ... 100 13selecting default shared_buffers ... 128MB 14selecting dynamic shared memory implementation ... posix 15creating configuration files ... ok 16running bootstrap script ... ok 17performing post-bootstrap initialization ... ok 18syncing data to disk ... ok 19 20WARNING: enabling "trust" authentication for local connections 21You can change this by editing pg_hba.conf or using the option -A, or 22--auth-local and --auth-host, the next time you run initdb. 23 24Success. You can now start the database server using: 25 26 pg_ctl -D /data/pgdata/ -l logfile start 27

修改配置文件

1cd /data/pgdata/ 2vi postgresql.conf 3# 将该条配置反注释,将localhost改成* 4listen_addresses = '*'

修改访问控制文件

1vi pg_hba.conf 2# IPv4 local connections: 3host all all 0.0.0.0/0 md5 4

说明:

1# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi", 2# "ident", "peer", "pam", "ldap", "radius" or "cert". Note that 3# "password" sends passwords in clear text; "md5" is preferred since 4# it sends encrypted passwords. 5

关闭防火墙

1systemctl stop firewalld 2systemctl disable firewalld

2.3.8 启动数据库

1[postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start 2server starting

进入数据库修改postgres密码

1psql 2postgres=# ALTER USER postgres WITH PASSWORD 'postgres'; 3ALTER ROLE

这样就算全部完成了,远程也可以连接数据库了。

2.4 RPM安装

2.4.1 下载rpm包

使用rpm安装需要下载上面所列出的4个rpm包,下载的时候需要注意版本的一致。

1wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-9.6.13-1PGDG.rhel7.x86_64.rpm 2wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-libs-9.6.13-1PGDG.rhel7.x86_64.rpm 3wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-contrib-9.6.13-1PGDG.rhel7.x86_64.rpm 4wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-server-9.6.13-1PGDG.rhel7.x86_64.rpm 5

2.4.2 安装依赖包

1yum install gcc make openssl zlib-devel libxslt -y 2

2.4.3 安装rpm包

注意,需要依次安装,包之间有依赖关系。

1[root@mydb ~]# rpm -ivh postgresql96-libs-9.6.13-1PGDG.rhel7.x86_64.rpm 2warning: postgresql96-libs-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY 3Preparing... ################################# [100%] 4Updating / installing... 5 1:postgresql96-libs-9.6.13-1PGDG.rh################################# [100%] 6[root@mydb ~]# rpm -ivh postgresql96-9.6.13-1PGDG.rhel7.x86_64.rpm 7warning: postgresql96-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY 8Preparing... ################################# [100%] 9Updating / installing... 10 1:postgresql96-9.6.13-1PGDG.rhel7 ################################# [100%] 11[root@mydb ~]# rpm -ivh postgresql96-server-9.6.13-1PGDG.rhel7.x86_64.rpm 12warning: postgresql96-server-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY 13Preparing... ################################# [100%] 14Updating / installing... 15 1:postgresql96-server-9.6.13-1PGDG.################################# [100%] 16[root@mydb ~]# rpm -ivh postgresql96-contrib-9.6.13-1PGDG.rhel7.x86_64.rpm 17warning: postgresql96-contrib-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY 18Preparing... ################################# [100%] 19Updating / installing... 20 1:postgresql96-contrib-9.6.13-1PGDG################################# [100%] 21

2.4.4 创建目录

1mkdir -p /data/pgdata 2chown -R postgres:postgres /data/pgdata/ 3chmod -R 775 /data/pgdata/

配置环境变量

1cd /home/postgres 2vi .bash_profile 3# 在path后面添加数据库的bin目录方便启动数据库 4PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/pgsql-9.6/bin/

2.4.5 初始化数据库

1su - postgres 2cd /usr/pgsql-9.6/bin/ 3initdb -D /data/pgdata/

2.4.6 配置启动数据库

修改配置文件

1cd /data/pgdata/ 2vi postgresql.conf 3# 将该条配置反注释,将localhost改成* 4listen_addresses = '*'

修改访问控制文件

1vi pg_hba.conf 2# IPv4 local connections: 3host all all 0.0.0.0/0 md5 4

说明:

1# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi", 2# "ident", "peer", "pam", "ldap", "radius" or "cert". Note that 3# "password" sends passwords in clear text; "md5" is preferred since 4# it sends encrypted passwords. 5

关闭防火墙

1systemctl stop firewalld 2systemctl disable firewalld

2.4.7 启动数据库

1[postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start 2server starting

进入数据库修改postgres密码

1psql 2postgres=# ALTER USER postgres WITH PASSWORD 'postgres'; 3ALTER ROLE

这样就算全部完成了,远程也可以连接数据库了。

3 总结

以上三种方法就是最常用的安装数据库的方式,当然也有更为简单的是直接yum install postgresql,但是这种方法只适合临时进行部署一个,不推荐作为生产环境,一般生产环境还会根据实际情况配置高可用的主从方式,采用流复制的方式来提高数据库可用性。

问题: 如果执行psql出现了
./psql: error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory
解决办法:在postgres的家目录中.bashrc中添加export LD_LIBRARY_PATH=/usr/local/pgsql/lib,然后source一下就可以了。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

PostgreSQL数据库安装方法总结 - HelloWorld