PostgreSQL数据库安装方法总结
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PostgreSQL数据库安装方法总结,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含9128字,纯文字阅读大概需要14分钟。
内容图文
![PostgreSQL数据库安装方法总结](/upload/InfoBanner/zyjiaocheng/909/ab28195d5fc24ea8ac14527b6d70f812.jpg)
https://mp.weixin.qq.com/s/emtwbvqKBb1h_3VJCVI8TA
1 引言
PostgreSQL数据库现在变得越来越流行,在DB-Engines网站(https://db-engines.com/en/ranking ),排名第四,本人所在的公司也是极力推广PG数据库,开源社区中,PG的活跃度也是非常高,本文简单介绍一下pg数据库的几种安装方法。
2 安装
2.1 部署规划
本文总结了PostgreSQL数据库的三种安装方法,都是基于CentOS7.6版本来进行,可以通过/etc/redhat-release来查看具体的版本。
[root@pgDatabase ~]# cat /etc/redhat-release
CentOS 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用户
useradd postgres
passwd postgres
登陆一下,让系统创建家目录
su - postgres
2.2.2 创建目录
mkdir -p /data/pgdata
chown -R postgres:postgres /data/pgdata/
chmod -R 775 /data/pgdata/
2.2.3 解压安装
tar -xzvf postgresql-9.6.13-4-linux-x64-binaries.tar.gz -C /data/
chown -R postgres /data/pgsql
解压后的文件夹名字是pgsql
创建软连接
cd /usr/local
ln -s /data/pgsql pgsql
配置环境变量
cd /home/postgres
vi .bash_profile
# 在path后面添加数据库的bin目录方便启动数据库
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/pgsql/bin
2.2.4 初始化数据库
su - postgres
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
2.2.5 启动数据库
[postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start
server starting
进入数据库修改postgres密码
psql
postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
ALTER 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检查。
yum install gcc make openssl zlib-devel -y
2.3.3 解压安装包
tar -xzvf postgresql-9.6.13.tar.gz -C /usr/local
2.3.4 编译并安装
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
2.3.5 创建postgres用户
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
2.3.6 创建目录
mkdir -p /data/pgdata
chown -R postgres:postgres /data/pgdata/
chmod -R 775 /data/pgdata/
2.3.7 初始化数据库
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
2.3.8 启动数据库
[postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start
server starting
进入数据库修改postgres密码
psql
postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
ALTER ROLE
这样就算全部完成了,远程也可以连接数据库了。
2.4 RPM安装
2.4.1 下载rpm包
使用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
2.4.2 安装依赖包
yum install gcc make openssl zlib-devel -y
2.4.3 安装rpm包
注意,需要依次安装,包之间有依赖关系。
[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%]
2.4.4 创建目录
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/
2.4.5 初始化数据库
su - postgres
cd /usr/pgsql-9.6/bin/
initdb -D /data/pgdata/
2.4.6 配置启动数据库
修改配置文件
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
2.4.7 启动数据库
[postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start
server starting
进入数据库修改postgres密码
psql
postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
ALTER ROLE
这样就算全部完成了,远程也可以连接数据库了。
3 总结
以上三种方法就是最常用的安装数据库的方式,当然也有更为简单的是直接yum install postgresql,但是这种方法只适合临时进行部署一个,不推荐作为生产环境,一般生产环境还会根据实际情况配置高可用的主从方式,采用流复制的方式来提高数据库可用性。
内容总结
以上是互联网集市为您收集整理的PostgreSQL数据库安装方法总结全部内容,希望文章能够帮你解决PostgreSQL数据库安装方法总结所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。