代码改变世界

PostgreSQL 10编译安装(CentOS 7)

2019-07-22 16:53  TankDBA  阅读(879)  评论(0编辑  收藏  举报

  版本说明:      

    Postgres 10.9

    CentOS 7.6

  1 安装必要软件

# yum groupinstall -y "Development tools"
 
# yum install -y bison flex readline-devel zlib-devel gcc

  2 获取Postgres资源并编译安装

  可通过访问https://www.postgresql.org/ftp/source/确定所需版本,以下使用10.9版本进行安装。

# pwd
 
/opt
 
# wget https://ftp.postgresql.org/pub/source/v10.9/postgresql-10.9.tar.gz
 
# tar xf postgresql-10.9.tar.gz
 
# cd postgresql-10.9/
# ./configure --prefix=/opt/pg10/
 
(部分输出内容省略)
 
config.status: creating GNUmakefile
 
config.status: creating src/Makefile.global
 
config.status: creating src/include/pg_config.h
 
config.status: creating src/include/pg_config_ext.h
 
config.status: creating src/interfaces/ecpg/include/ecpg_config.h
 
config.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.s
 
config.status: linking src/backend/port/dynloader/linux.c to src/backend/port/dynloader.c
 
config.status: linking src/backend/port/posix_sema.c to src/backend/port/pg_sema.c
 
config.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c
 
config.status: linking src/backend/port/dynloader/linux.h to src/include/dynloader.h
 
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
 
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port
# make && make install
 
(部分输出内容省略)
 
make[1]: Leaving directory `/opt/postgresql-10.9/src'
make -C config install
make[1]: Entering directory `/opt/postgresql-10.9/config'
/usr/bin/mkdir -p '/opt/pg10/lib/postgresql/pgxs/config'
/usr/bin/install -c -m 755 ./install-sh '/opt/pg10/lib/postgresql/pgxs/config/install-sh'
/usr/bin/install -c -m 755 ./missing '/opt/pg10/lib/postgresql/pgxs/config/missing'
make[1]: Leaving directory `/opt/postgresql-10.9/config'
 
PostgreSQL installation complete.
 
 
 
# /opt/pg10/bin/postgres --version
 
postgres (PostgreSQL) 10.9

   3 创建用户

# groupadd -g 2000 postgres
 
# useradd -g 2000 -u 2000 postgres
 
# id postgres
 
# uid=2000(postgres) gid=2000(postgres) groups=2000(postgres)

  4 创建路径及权限修改

# mkdir -p /pgdata/10/{data,backups,scripts,archive_wals}
 
# chown -R postgres:postgres /pgdata/10
 
# chown -R postgres:postgres /opt/pg10
 
# chmod 0700 /pgdata/10/data

  5 环境变量

# su - postgres
 
$ cat .bash_profile
 
# Get the aliases and functions
 
if [ -f ~/.bashrc ]; then
 
. ~/.bashrc
 
fi
 
 
 
# User specific environment and startup programs
 
 
 
export PATH
 
export PGHOME=/opt/pg10
 
export PGDATA=/pgdata/10
 
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin
 
 
 
$ source .bash_profile

  6 初始化数据库

$ /opt/pg10/bin/initdb -D /pgdata/10/data/ -W
 
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.
 
 
 
Enter new superuser password:
 
Enter it again:
 
 
 
fixing permissions on existing directory /pgdata/10/data ... ok
 
creating subdirectories ... ok
 
selecting default max_connections ... 100
 
selecting default shared_buffers ... 128MB
 
selecting default timezone ... PRC
 
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:
 
 
 
    /opt/pg10/bin/pg_ctl -D /pgdata/10/data/ -l logfile start

  7 启动&关闭

--启动命令
 
$ pg_ctl -D /pgdata/10/data start
 
waiting for server to start....2019-07-19 18:24:02.926 CST [31065] LOG:  listening on IPv6 address "::1", port 5432
 
2019-07-19 18:24:02.926 CST [31065] LOG:  listening on IPv4 address "127.0.0.1", port 5432
 
2019-07-19 18:24:02.929 CST [31065] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
 
2019-07-19 18:24:02.943 CST [31066] LOG:  database system was shut down at 2019-07-19 18:21:30 CST
 
2019-07-19 18:24:02.946 CST [31065] LOG:  database system is ready to accept connections
 
 done
 
server started
 
 
 
--查看后台进程
 
$ ps -ef|grep postgres:
 
postgres  31100  31098  0 18:25 ?        00:00:00 postgres: checkpointer process 
 
postgres  31101  31098  0 18:25 ?        00:00:00 postgres: writer process 
 
postgres  31102  31098  0 18:25 ?        00:00:00 postgres: wal writer process 
 
postgres  31103  31098  0 18:25 ?        00:00:00 postgres: autovacuum launcher process 
 
postgres  31104  31098  0 18:25 ?        00:00:00 postgres: stats collector process 
 
postgres  31105  31098  0 18:25 ?        00:00:00 postgres: bgworker: logical replication launcher
 
postgres  31107  30025  0 18:25 pts/1    00:00:00 grep --color=auto postgres:
 
 
 
--登录验证
 
$ /opt/pg10/bin/pg_isready -p 5432
 
/tmp:5432 - accepting connections
 
 
 
$ psql -p 5432 -U postgres -d postgres
 
psql (10.9)
 
Type "help" for help.
 
 
 
postgres=# \q
 
$
--关闭命令
 
$ pg_ctl -D /pgdata/10/data/ -ms stop

  8 修改白名单

  PG默认不允许远程访问数据库,可以通过修改监听地址、修改pg_hba.conf文件来实现远程访问。

--修改监听地址
 
将配置文件中listen_addresses的值由'localhost'修改为'listen_addresses'。
 
$ cp /pgdata/10/data/postgresql.conf /pgdata/10/data/postgresql.conf.bak
 
$ grep listen /pgdata/10/data/postgresql.conf
 
listen_addresses = '*'                # what IP address(es) to listen on;
$ pg_ctl -D /pgdata/10/data/ -ms stop
$ pg_ctl -D /pgdata/10/data start
 
--修改pg_hba.conf文件
 
$ cp /pgdata/10/data/pg_hba.conf /pgdata/10/data/pg_hba.conf.bak
 
$ echo "host postgres postgres 0.0.0.0/0 md5" >> /pgdata/10/data/pg_hba.conf
 
--重新加载配置文件
 
$  /opt/pg10/bin/pg_ctl -D /pgdata/10/data/ reload
 
server signaled


参考资料:

  《PostgreSQL实战》-第一章 
  《PostgreSQL 10.1手册》-III.服务器管理-16.从源码安装
  https://blog.csdn.net/helloworld_dream/article/details/81483235
  https://www.cnblogs.com/tplife2019/p/10234275.html
  http://www.mamicode.com/info-detail-2373348.html

Tank
2019.7.19

https://blog.csdn.net/daiyejava

https://www.cnblogs.com/okey