Pg12/Centos7 源码编译安装部署文档

========================== 环境准备 ==========================

1、下载安装包到/opt目录:
wget https://ftp.postgresql.org/pub/source/v12.1/postgresql-12.1.tar.gz

2、解压:tar -xvf postgresql-12.1.tar.gz

3、安装依赖包:
yum groupinstall "Development tools"
yum install -y bison flex readline-devel zlib-devel

========================== 编译安装 ==========================

4、设置软连接,个人习惯放到/usr/local目录下:
[root@otter3 opt]# cd /usr/local/
[root@otter3 local]# ln -s /opt/postgresql-12.1 pg12

5、进入目录编译安装:
[root@otter3 local]# cd /usr/local/pg12
[root@otter3 pg12]# ./configure --prefix=/usr/local/pg12/ --with-pgport=1921

如果报错:configure: error: no acceptable C compiler found in $PATH
安装:yum install gcc可以解决

执行编译:gmake world -- 如果看到PostgreSQL, contrib, and documentation successfully made. Ready to install 说明成功
执行安装:gmake install-world -- 如果看到PostgreSQL, contrib, and documentation installation complete 说明成功
注:如果报错软件包不存在yum安装即可

6、进入安装目录执行查看版本命令:
[root@otter3 pg12]# ./bin/postgres --version
postgres (PostgreSQL) 12.1

========================== DB初始化 ==========================

7、创建用户,用来管理pg
[root@otter3 bin]# groupadd -g 2000 postgres
[root@otter3 bin]# useradd -g 2000 -u 2000 postgres
[root@otter3 bin]# id postgres
uid=2000(postgres) gid=2000(postgres) groups=2000(postgres)

8、创建数据库目录(数据目录安装在data目录下,这些都是后期必用的文件夹):
[root@otter3 pgdata]# mkdir -p /data/pgdata/pg12/{data,backups,scripts,archive_wals}

9、目录赋权(后期即使用initdb工具初始化时会自动回收非postgres用户权限,但这里先手动赋权养成好习惯):
[root@otter3 pgdata]# chown -R postgres:postgres /data/pgdata/pg12
[root@otter3 pgdata]# chmod 0700 /data/pgdata/pg12

10、切换postgres用户并初始化数据库:
[root@otter3 bin]# su - postgres
[postgres@otter3 ~]$ /usr/local/pg12/bin/initdb -D /data/pgdata/pg12/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 /data/pgdata/pg12/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: 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:
/usr/local/pg12/bin/pg_ctl -D /data/pgdata/pg12/data -l logfile start

注:因为初始化时指定了参数W,所以在initdb工具初始化时要求创建密码

11、根据提示启动数据库命令启动数据库:/usr/local/pg12/bin/pg_ctl -D /data/pgdata/pg12/data -l logfile start

12、查看数据库状态:
[postgres@otter3 ~]$ /usr/local/pg12/bin/pg_ctl -D /data/pgdata/pg12/data status
pg_ctl: server is running (PID: 30677)
/opt/postgresql-12.1/bin/postgres "-D" "/data/pgdata/pg12/data"

13、使用工具检测数据库是否接受连接:
[postgres@otter3 ~]$ /usr/local/pg12/bin/pg_isready -p 1921
/tmp:1921 - accepting connections

========================== 启动关闭 ==========================

14、启动方式1:[postgres@otter3 ~]$ /usr/local/pg12/bin/pg_ctl -D /data/pgdata/pg12/data start

启动方式2:[postgres@otter3 ~]$ /usr/local/pg12/bin/postgres -D /data/pgdata/pg12/data &

关闭方式1:[postgres@otter3 ~]$ /usr/local/pg12/bin/pg_ctl -D /data/pgdata/pg12/data start

关闭方式2:kill -sigterm head -1 /data/pgdata/pg12/data/postmaster.pid

15、配置服务器脚本拷贝启动脚本并命名为pg12:
root用户下进入目录:
[postgres@otter3 ~]$ exit
logout
[root@otter3 pgdata]# cd /usr/local/pg12/contrib/start-scripts
[root@otter3 start-scripts]# cp linux /etc/init.d/pg12
赋权并查看:
[root@otter3 init.d]# chmod +x /etc/init.d/pg12
[root@otter3 init.d]# ls -lh /etc/init.d/pg12
设置开启启动/关闭:
[root@otter3 init.d]# chkconfig pg12 on -- 关闭 off
[root@otter3 init.d]# chkconfig --list | grep pg12

========================== 环境变量 ==========================
用postgres用户设置环境变量,编辑 .bash_profile 文件添加一下内容:
export PGDATA=/data/pgdata/pg12/data
export PATH=/usr/local/pg12/bin:$PATH
export LD_LIBRARY=/usr/local/pg12/lib:$LD_LIBRARY_PATH
export LANG=en_US.UTF-8

source .bash_profile
========================== 远程访问 ==========================

16、数据库配置基础:
重要配置文件(postgresql.conf:主要负责:文件位置、资源限制、复制集群等
pg_hba.conf:主要负责:客户端连接和认证-- 数据库的防火墙)

17、配置远程访问:
默认情况下pg不允许通过远程访问数据库:
[postgres@otter3 data]$ netstat -nlt | grep 1921
tcp 0 0 0.0.0.0:1921 0.0.0.0:* LISTEN
tcp6 0 0 :::1921 :::* LISTEN
通过以下方式可以修改:
[root@otter3 init.d]# vim /data/pgdata/pg12/data/postgresql.conf
1、修改监听地址为*
listen_addresses = '*' # what IP address(es) to listen on;
                                    # comma-separated list of addresses;
                                    # defaults to 'localhost'; use '*' for all
                                    # (change requires restart)

#port = 1921 # (change requires restart)

注意:修改配置文件需要重启(注意切换用户postgres),命令参考:/usr/local/pg12/bin/pg_ctl -D /data/pgdata/pg12/data restart
同时修改:pg_hba.conf
[root@otter3 init.d]# echo "host all all 0.0.0.0/0 md5" >> /data/pgdata/pg12/data/pg_hba.conf
[root@otter3 init.d]# /usr/local/pg12/bin/pg_ctl -D /data/pgdata/pg12/data reload

========================== OVER ==========================

你可能感兴趣的:(Pg12/Centos7 源码编译安装部署文档)