PostgreSQL数据库如今变得愈来愈流行,在DB-Engines网站(https://db-engines.com/en/ranking ),排名第四,本人所在的公司也是极力推广PG数据库,开源社区中,PG的活跃度也是很是高,本文简单介绍一下pg数据库的几种安装方法。linux
本文总结了PostgreSQL数据库的三种安装方法,都是基于CentOS7.6版原本进行,能够经过/etc/redhat-release来查看具体的版本。sql
[root@pgDatabase ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
对于数据库的目录规划,并无严格的规定,可是生产环境中,仍是建议将数据目录放在挂载的盘中,防止因为系统的问题致使数据库没法启动,数据丢失。数据库
目录 | 存放位置 |
---|---|
home目录 | /home/postgres |
安装目录 | /usr/local/ |
数据目录 | /data/pgdata |
安装文件介绍和下载连接,默认版本为pg9.6。bootstrap
安装方式 | 安装包名称 | 下载地址 |
---|---|---|
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/ |
useradd postgres passwd postgres
登录一下,让系统建立家目录bash
su - postgres
mkdir -p /data/pgdata chown -R postgres:postgres /data/pgdata/ chmod -R 775 /data/pgdata/
tar -xzvf postgresql-9.6.13-4-linux-x64-binaries.tar.gz -C /data/ chown -R postgres /data/pgsql
解压后的文件夹名字是pgsql服务器
建立软链接ide
cd /usr/local ln -s /data/pgsql pgsql
配置环境变量post
cd /home/postgres vi .bash_profile # 在path后面添加数据库的bin目录方便启动数据库 PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/pgsql/bin
su - postgres initdb -D /data/pgdata/
修改配置文件网站
cd /data/pgdata/ vi postgresql.conf # 将该条配置反注释,将localhost改为* listen_addresses = '*'
修改访问控制文件this
vi pg_hba.conf # IPv4 local connections: host all all 0.0.0.0/0 md5
说明:
# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi", # "ident", "peer", "pam", "ldap", "radius" or "cert". Note that # "password" sends passwords in clear text; "md5" is preferred since # it sends encrypted passwords.
关闭防火墙
systemctl stop firewalld
[postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start server starting
进入数据库修改postgres密码
psql postgres=# ALTER USER postgres WITH PASSWORD 'postgres'; ALTER ROLE
这样就算所有完成了,远程也能够链接数据库了。
若是服务器能够联网,可使用wget命令来下载。或者从网站上下载而后传到服务器也能够。
wget http://ftp.postgresql.org/pub/source/v9.6.13/postgresql-9.6.13.tar.gz
安装前必需要保证gcc编译器已经安装好。可使用rpm -qa gcc检查。
yum install gcc make openssl zlib-devel -y
tar -xzvf postgresql-9.6.13.tar.gz -C /usr/local
cd /usr/local/postgresql-9.6.13/ ./configure --prefix=/usr/local/pgsql
问题
... configure: error: readline library not found If you have readline already installed, see config.log for details on the failure. It is possible the compiler isn't looking in the proper directory. Use --without-readline to disable readline support. ...
按照提示后面加上便可--without-readline
./configure --prefix=/usr/local/pgsql --without-readline
安装,该过程比较长,通常在5~10分钟左右,安装完后,会在/usr/local目录下看到pgsql文件夹,也就是安装完后的数据库的位置。
make && make install
useradd postgres passwd postgres
登录一下,让系统建立家目录
su - postgres
配置环境变量
cd /home/postgres vi .bash_profile # 在path后面添加数据库的bin目录方便启动数据库 PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/pgsql/bin
mkdir -p /data/pgdata chown -R postgres:postgres /data/pgdata/ chmod -R 700 /data/pgdata/
su - postgres initdb -D /data/pgdata/
输出如下内容则代表成功。
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 /data/pgdata ... 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 /data/pgdata/ -l logfile start
修改配置文件
cd /data/pgdata/ vi postgresql.conf # 将该条配置反注释,将localhost改为* listen_addresses = '*'
修改访问控制文件
vi pg_hba.conf # IPv4 local connections: host all all 0.0.0.0/0 md5
说明:
# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi", # "ident", "peer", "pam", "ldap", "radius" or "cert". Note that # "password" sends passwords in clear text; "md5" is preferred since # it sends encrypted passwords.
关闭防火墙
systemctl stop firewalld systemctl disable firewalld
[postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start server starting
进入数据库修改postgres密码
psql postgres=# ALTER USER postgres WITH PASSWORD 'postgres'; ALTER ROLE
这样就算所有完成了,远程也能够链接数据库了。
使用rpm安装须要下载上面所列出的4个rpm包,下载的时候须要注意版本的一致。
wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-9.6.13-1PGDG.rhel7.x86_64.rpm wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-libs-9.6.13-1PGDG.rhel7.x86_64.rpm wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-contrib-9.6.13-1PGDG.rhel7.x86_64.rpm wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-server-9.6.13-1PGDG.rhel7.x86_64.rpm
yum install gcc make openssl zlib-devel libxslt -y
注意,须要依次安装,包之间有依赖关系。
[root@mydb ~]# rpm -ivh postgresql96-libs-9.6.13-1PGDG.rhel7.x86_64.rpm warning: postgresql96-libs-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY Preparing... ################################# [100%] Updating / installing... 1:postgresql96-libs-9.6.13-1PGDG.rh################################# [100%] [root@mydb ~]# rpm -ivh postgresql96-9.6.13-1PGDG.rhel7.x86_64.rpm warning: postgresql96-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY Preparing... ################################# [100%] Updating / installing... 1:postgresql96-9.6.13-1PGDG.rhel7 ################################# [100%] [root@mydb ~]# rpm -ivh postgresql96-server-9.6.13-1PGDG.rhel7.x86_64.rpm warning: postgresql96-server-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY Preparing... ################################# [100%] Updating / installing... 1:postgresql96-server-9.6.13-1PGDG.################################# [100%] [root@mydb ~]# rpm -ivh postgresql96-contrib-9.6.13-1PGDG.rhel7.x86_64.rpm warning: postgresql96-contrib-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY Preparing... ################################# [100%] Updating / installing... 1:postgresql96-contrib-9.6.13-1PGDG################################# [100%]
mkdir -p /data/pgdata chown -R postgres:postgres /data/pgdata/ chmod -R 775 /data/pgdata/
配置环境变量
cd /home/postgres vi .bash_profile # 在path后面添加数据库的bin目录方便启动数据库 PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/pgsql-9.6/bin/
su - postgres cd /usr/pgsql-9.6/bin/ initdb -D /data/pgdata/
修改配置文件
cd /data/pgdata/ vi postgresql.conf # 将该条配置反注释,将localhost改为* listen_addresses = '*'
修改访问控制文件
vi pg_hba.conf # IPv4 local connections: host all all 0.0.0.0/0 md5
说明:
# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi", # "ident", "peer", "pam", "ldap", "radius" or "cert". Note that # "password" sends passwords in clear text; "md5" is preferred since # it sends encrypted passwords.
关闭防火墙
systemctl stop firewalld systemctl disable firewalld
[postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start server starting
进入数据库修改postgres密码
psql postgres=# ALTER USER postgres WITH PASSWORD 'postgres'; ALTER ROLE
这样就算所有完成了,远程也能够链接数据库了。
以上三种方法就是最经常使用的安装数据库的方式,固然也有更为简单的是直接yum install postgresql,可是这种方法只适合临时进行部署一个,不推荐做为生产环境,通常生产环境还会根据实际状况配置高可用的主从方式,采用流复制的方式来提升数据库可用性。