分享

Postgresql源码安装

 highoo 2019-05-19

以在64位CentOS6.5操作系统上源码安装postgresql-9.6beta1为例

 

一.进入官网下载代码(postgresql-9.6beta1.tar.gz)

https://www.

 

二.将源码上传到服务器/home/alian目录下

可以在windows安装ssh或xftp工具,也可以在CentOS安装lrzsz的rpm包(使用rz,sz命令)上传或下载文件。

 

三.将源码在当前目录下解压

[root@localhost alian]# tar xzvf postgresql-9.6beta1.tar.gz

 

四.进入解压后的目录postgresql-9.6beta1,使用configure工具查看编译帮助

[root@localhost postgresql-9.6beta1]# ./configure --help

 

五.根据帮助中各配置描述选择适合自己的配置选项

如下命令中

--prefix=/opt/pg9.6

pg软件的安装路径

 

以下4个配置不是必须的,但如果想修改必须要重新initdb

--with-segsize=1

表在操作系统上物理文件大小,单位GB,默认值为1,如果表大于1GB,将在操作系统上分割成多个1GB的文件。

--with-blocksize=32

块大小,单位KB,默认值为8,是表数据I/O和存储的单元,范围1-32,且必须是2的N次方。

--with-wal-segsize=32

WAL在操作系统上物理文件大小,单位MB,默认值16,范围1-64,且必须是2的N次方。

--with-wal-blocksize=64

WAL块大小,单位KB,是WAL I/O和存储的单元,范围1-64,且必须是2的N次方。

复制代码
[root@localhost postgresql-9.6beta1]# ./configure --prefix=/opt/pg9.6 --with-segsize=1 --with-blocksize=32 --with-wal-segsize=32 --with-wal-blocksize=64


checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether to build with 64-bit integer date/time support... yes
checking whether NLS is wanted... no
checking for default port number... 5432
checking for block size... 32kB
checking for segment size... 1GB
checking for WAL block size... 64kB
checking for WAL segment size... 32MB
checking for gcc... gcc

..............
复制代码

 

 

configure过程中可能会出现下面的错误,原因是缺少对应的库,安装对应的库即可。

复制代码
configure: error: readline library not found

[root@localhost postgresql-9.6beta1]# yum install readline-devel

 configure: error: zlib library not found

[root@localhost postgresql-9.6beta1]# yum install zlib-devel
复制代码

 

 

六.configure成功后,可以直接执行make命令进行简单编译,也可以执行make world将contrib目录下的扩展工具一起编译,当然也可以先进行简单编译,然后进入contrib下对某一工具进行单独编译

方法一:简单安装

1.执行命令

[root@localhost postgresql-9.6beta1]# make && make install

命令执行完之后,pg软件就被安装到/opt/pg9.6路径下

[root@localhost postgresql-9.6beta1]# ls /opt/pg9.6/
bin include lib share

 

2.如果想安装contrib下的某一扩展工具,以检测数据库运行的pg_stat_statements为例

进入contrib/pg_stat_statements目录下(前提是已在主目录下执行了configure)

[root@localhost postgresql-9.6beta1]# cd contrib/pg_stat_statements/

 

执行命令

[root@localhost pg_stat_statements]# make && make install

 

方法二、全部安装(contrib下所有的扩展工具)

[root@localhost postgresql-9.6beta1]# make world && make install-world

 

七、软件安装完成后,进行初始化

1.新建一个用户postgres

[root@localhost postgresql-9.6beta1]# groupadd postgres

[root@localhost postgresql-9.6beta1]# useradd postgres

 

2.创建PGDATA目录

[root@localhost postgresql-9.6beta1]# mkdir -p /mnt/pgdata
[root@localhost postgresql-9.6beta1]# chown postgres:postgres /mnt/pgdata/

 

3.使用postgres用户初始化

[root@localhost postgresql-9.6beta1]# su postgres

[postgres@localhost postgresql-9.6beta1]$ /opt/pg9.6/bin/initdb -D /mnt/pgdata -E UTF8 --locale=C

 

4.使用postgres用户启停数据库

复制代码
[postgres@localhost postgresql-9.6beta1]$ /opt/pg9.6/bin/pg_ctl -D /mnt/pgdata/ start
server starting

[postgres@localhost postgresql-9.6beta1]$ /opt/pg9.6/bin/pg_ctl -D /mnt/pgdata/ stop
waiting for server to shut down....LOG: database system is shut down
done
server stopped
复制代码

 

5.修改配置文件,使用客户端访问数据库

初始化完成后,会自动为数据库创建一个超级用户postgres(执行initdb的用户),但是数据库还没有给该用户设置访问密码,所以启动数据库后,执行下面的命令,给该用户配置一个密码

复制代码
[postgres@localhost postgresql-9.6beta1]$ /opt/pg9.6/bin/psql -U postgres
psql (9.6beta1)
Type "help" for help.

postgres=# alter user postgres with password 'password';
ALTER ROLE
postgres=# \q
复制代码

 

另外,在PGDATA目录下生成配置文件pg_hba.conf和postgresql.conf,

postgresql.conf中配置#listen_addresses = 'localhost',表示只监听本地连接,将此配置改成*用来监听所有IP的访问,也可以指定特定的IP(注意前面的注释符#删掉)

listen_addresses = '*'

 

pg_hba.conf主要内容如下

复制代码
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
复制代码

 

其中trust表示允许无密码访问,这是不安全的,将其改为md5,使用密码访问。

复制代码
# "local" is for Unix domain socket connections only

local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
复制代码

 

修改完密码和配置文件后重启服务再次访问数据库时就会提示输入密码

复制代码
[postgres@localhost postgresql-9.6beta1]$ /opt/pg9.6/bin/psql -U postgres
Password for user postgres: 
psql (9.6beta1)
Type "help" for help.

postgres=#
复制代码

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多