记录centos7 安装postgresql10.1

官网地址https://www.postgresql.org/download/linux/redhat/

  1. yum安装上面写的很清楚了,按照步骤来就可以。

    《记录centos7 安装postgresql10.1》 7FDA48BD-5C94-4ECC-9A15-E370E9EFC9E7.png

  2. 编译安装
    1.下载地址https://www.postgresql.org/ftp/source
    2.将文件放到任意目录 解压 tar -zxvf ./postgresql-9.5.5.tar.gz
    3.进入到解压目录, 编译到指定目录
    ./configure –prefix=/usr/local/postgresql/postgresql10.1
    但发现最下面有error。
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.

执行find / -name readline* 或者rpm -qa | grep readline (rpm -ql readline) 发现是有readline文件的 。
这里结合上次编译安装nginx经验,是缺少devel包导致的。
就百度了一下devel是什么。C语言开发Header头文件和库,使用源码编译需要devel开发包。
附上地址

linux devel包 和 非devel包的区别
转载 2013年11月18日 18:15:37 14069
devel 包主要是供开发用,至少包括以下2个东西:
1. 头文件
2. 链接库
有的还含有开发文档或演示代码。
以 glib 和 glib-devel 为例:
如果你安装基于 glib 开发的程序,只需要安装 glib 包就行了。
但是如果你要编译使用了 glib 的源代码,则需要安装 glib-devel。

3.yum install readline-devel
前提系统安装了gcc 和zlib 库
安装方法 yum install -y gcc gcc-c++
yum install -y zlib zlib-devel
4.再次编译
./configure –prefix=/usr/local/postgresql/postgresql10.1
可以看到有configure: creating ./config.status信息
说明配置文件已经创建。
5.编译安装
make&&make install

make[1]: 离开目录“/usr/local/tool/postgresql-10.1/config”
PostgreSQL installation complete.

最后显示PostgreSQL installation complete.说明安装成功。
6.创建一个普通用户,因为postgresql安装默认的超级用户为postgres,所以要创建一个用户来启动数据库。
useradd postgres
7.修改postgresql安装文件的文件权限给新加用户postgres
chown -R postgres:postgres /usr/local/postgresql/postgresql10.1
8.创建环境变量
切换用户su postgres
编辑文件 vi .bash_profile 如下

PGHOME=/usr/local/postgresql/postgresql10.1
export PGHOME
PGDATA=/usr/local/postgresql/postgresql10.1/data
export PGDATA

PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin

export PATH

使之生效 source .bash_profile
查看效果

[postgres@localhost ~]$ psql -V
psql (PostgreSQL) 10.1

9.初始化数据库
initdb
由于设置了环境变量PGDATA,所以数据库目录默认为PGDATA指定目录

Success. You can now start the database server using:

    pg_ctl -D /usr/local/postgresql/postgresql10.1/data -l logfile start

[postgres@localhost ~]$ ls /usr/local/postgresql/postgresql10.1/
bin  data  include  lib  share
[postgres@localhost ~]$ ls /usr/local/postgresql/postgresql10.1/data
base          pg_ident.conf  pg_serial     pg_tblspc    postgresql.auto.conf
global        pg_logical     pg_snapshots  pg_twophase  postgresql.conf
pg_commit_ts  pg_multixact   pg_stat       PG_VERSION
pg_dynshmem   pg_notify      pg_stat_tmp   pg_wal
pg_hba.conf   pg_replslot    pg_subtrans   pg_xact

base 是表空间目录,
global 是相关全局变量的目录,
pg_hba.conf是访问控制配置(127.0.0.1改为信任的客户端ip网段使其可以远程访问)
postgresql.conf 是postgresql主配置文件(listen_address=localhost改为星号使其监听整个网络)
10.修改配置文件

修改postgresql.conf
1. listen_addresses = '*'
2. port = 5432
修改pg_hba.conf
# "local" is for Unix domain socket connections only
local   all             all                                  trust
# IPv4 local connections:
host    all             all             0.0.0.0/0            trust

11.启动postgresql 并指定日志文件
pg_ctl start -l /usr/local/postgresql/postgresql10.1/logs/pg_server.log

[postgres@localhost postgresql10.1]$ mkdir logs
[postgres@localhost postgresql10.1]$ mkdir pg_server.log
[postgres@localhost postgresql10.1]$ pg_ctl start -l /usr/local/postgresql/postgresql10.1/logs/pg_server.log
waiting for server to start.... done
server started

12.连接数据库并设置密码

[postgres@localhost postgresql10.1]$ psql
psql (10.1)
Type "help" for help.

postgres=# \password
Enter new password: 
Enter it again: 
postgres=# \l

13.开放端口

[root@localhost postgresql10.1]# firewall-cmd --zone=public --add-port=5432/tcp --permanent
success
[root@localhost postgresql10.1]# firewall-cmd --reload
success
[root@localhost postgresql10.1]# 

14.设置为服务
PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下,其中linux文件就是启动脚本

[root@localhost contrib]# cd start-scripts/
[root@localhost start-scripts]# ls
freebsd  linux  osx

1.修改linux文件权限(切换到root用户进行操作)
[root@localhost start-scripts]# chmod a+x linux
2.复制linux文件到/etc/init.d目录下,更名为postgresql
[root@localhost start-scripts]# cp linux /etc/init.d/postgresql
3.修改/etc/init.d/postgresql文件的两个变量
prefix设置为postgresql的安装路径:/usr/local/postgresql/postgresql10.1
PGDATA设置为postgresql的数据目录路径:/usr/local/postgresql/postgresql10.1/data
4.执行service postgresql restart,重启PostgreSQL服务
这里碰到个问题-bash: /usr/local/postgresql/postgresql10.1/data/logs/serverlog: Permission denied
自己创建logs/serverlog并修改权限chown -R postgres:root logs/

15.设置postgresql服务开机自启动chkconfig postgresql on

[root@localhost logs]# chkconfig postgresql on
[root@localhost logs]# chkconfig --list postgresql
postgresql      0:关 1:关 2:开 3:开 4:开 5:开 6:关

ok,至此服务端安装完成,可以尝试远程连接了。

    原文作者:乌托邦缤果
    原文地址: https://www.jianshu.com/p/84d93da2f8f8
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞