Centos 7 安装postgres-13.2

准备工作

1、创建用户和配置环境参数

(1)、创建用户组和创建所需目录

[root@jruing ~]# groupadd postgres
[root@jruing ~]# useradd -d /home/postgres -g postgres -m postgres
[root@jruing ~]# chmod 755 /home/postgres
[root@jruing ~]# mkdir -p /home/postgres/software
[root@jruing ~]# mkdir -p /home/postgres/yunwei
[root@jruing ~]# mkdir -p /data/postgres/postgres-13.2/data
[root@jruing ~]# mkdir -p /data/postgres/postgres-13.2/logs
[root@jruing ~]# mkdir -p /data/postgres/postgres-13.2/tablespace
[root@jruing ~]# chown -R postgres:postgres /data/postgres
[root@jruing ~]# chown -R postgres:postgres /home/postgres

(2)、通过yum安装必要依赖

<1>、安装gcc相关依赖
[root@jruing ~]# yum install gcc*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
centos                                                                                                                                                                                                                | 3.6 kB  00:00:00     
Package gcc-4.8.5-28.el7.x86_64 already installed and latest version
Package gcc-gfortran-4.8.5-28.el7.x86_64 already installed and latest version
Package gcc-c++-4.8.5-28.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package gcc-gnat.x86_64 0:4.8.5-28.el7 will be installed
--> Processing Dependency: libgnat-devel = 4.8.5-28.el7 for package: gcc-gnat-4.8.5-28.el7.x86_64
--> Processing Dependency: libgnat = 4.8.5-28.el7 for package: gcc-gnat-4.8.5-28.el7.x86_64
---> Package gcc-objc.x86_64 0:4.8.5-28.el7 will be installed
--> Processing Dependency: libobjc = 4.8.5-28.el7 for package: gcc-objc-4.8.5-28.el7.x86_64
--> Processing Dependency: libobjc.so.4()(64bit) for package: gcc-objc-4.8.5-28.el7.x86_64
---> Package gcc-objc++.x86_64 0:4.8.5-28.el7 will be installed
--> Running transaction check
---> Package libgnat.x86_64 0:4.8.5-28.el7 will be installed
---> Package libgnat-devel.x86_64 0:4.8.5-28.el7 will be installed
---> Package libobjc.x86_64 0:4.8.5-28.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================================================================
 Package                                                      Arch                                                  Version                                                      Repository                                             Size
=============================================================================================================================================================================================================================================
Installing:
 gcc-gnat                                                     x86_64                                                4.8.5-28.el7                                                 centos                                                 13 M
 gcc-objc                                                     x86_64                                                4.8.5-28.el7                                                 centos                                                5.7 M
 gcc-objc++                                                   x86_64                                                4.8.5-28.el7                                                 centos                                                6.1 M
Installing for dependencies:
 libgnat                                                      x86_64                                                4.8.5-28.el7                                                 centos                                                965 k
 libgnat-devel                                                x86_64                                                4.8.5-28.el7                                                 centos                                                2.7 M
 libobjc                                                      x86_64                                                4.8.5-28.el7                                                 centos                                                 78 k

Transaction Summary
=============================================================================================================================================================================================================================================
Install  3 Packages (+3 Dependent packages)

Total download size: 29 M
Installed size: 87 M
Is this ok [y/d/N]: y
Downloading packages:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                         44 MB/s |  29 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libgnat-4.8.5-28.el7.x86_64                                                                                                                                                                                               1/6 
  Installing : libobjc-4.8.5-28.el7.x86_64                                                                                                                                                                                               2/6 
  Installing : gcc-objc-4.8.5-28.el7.x86_64                                                                                                                                                                                              3/6 
  Installing : libgnat-devel-4.8.5-28.el7.x86_64                                                                                                                                                                                         4/6 
  Installing : gcc-gnat-4.8.5-28.el7.x86_64                                                                                                                                                                                              5/6 
  Installing : gcc-objc++-4.8.5-28.el7.x86_64                                                                                                                                                                                            6/6 
  Verifying  : libgnat-devel-4.8.5-28.el7.x86_64                                                                                                                                                                                         1/6 
  Verifying  : libobjc-4.8.5-28.el7.x86_64                                                                                                                                                                                               2/6 
  Verifying  : gcc-objc-4.8.5-28.el7.x86_64                                                                                                                                                                                              3/6 
  Verifying  : libgnat-4.8.5-28.el7.x86_64                                                                                                                                                                                               4/6 
  Verifying  : gcc-gnat-4.8.5-28.el7.x86_64                                                                                                                                                                                              5/6 
  Verifying  : gcc-objc++-4.8.5-28.el7.x86_64                                                                                                                                                                                            6/6 

Installed:
  gcc-gnat.x86_64 0:4.8.5-28.el7                                                gcc-objc.x86_64 0:4.8.5-28.el7                                                gcc-objc++.x86_64 0:4.8.5-28.el7                                               

Dependency Installed:
  libgnat.x86_64 0:4.8.5-28.el7                                               libgnat-devel.x86_64 0:4.8.5-28.el7                                               libobjc.x86_64 0:4.8.5-28.el7                                              

Complete!
<2>、安装readline-devel相关依赖
[root@jruing ~]# yum install readline-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package readline-devel.x86_64 0:6.2-10.el7 will be installed
--> Processing Dependency: ncurses-devel for package: readline-devel-6.2-10.el7.x86_64
--> Running transaction check
---> Package ncurses-devel.x86_64 0:5.9-14.20130511.el7_4 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================================================================
 Package                                                    Arch                                               Version                                                              Repository                                          Size
=============================================================================================================================================================================================================================================
Installing:
 readline-devel                                             x86_64                                             6.2-10.el7                                                           centos                                             138 k
Installing for dependencies:
 ncurses-devel                                              x86_64                                             5.9-14.20130511.el7_4                                                centos                                             712 k

Transaction Summary
=============================================================================================================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 851 k
Installed size: 2.4 M
Is this ok [y/d/N]: y
Downloading packages:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                        9.1 MB/s | 851 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : ncurses-devel-5.9-14.20130511.el7_4.x86_64                                                                                                                                                                                1/2 
  Installing : readline-devel-6.2-10.el7.x86_64                                                                                                                                                                                          2/2 
  Verifying  : readline-devel-6.2-10.el7.x86_64                                                                                                                                                                                          1/2 
  Verifying  : ncurses-devel-5.9-14.20130511.el7_4.x86_64                                                                                                                                                                                2/2 

Installed:
  readline-devel.x86_64 0:6.2-10.el7                                                                                                                                                                                                         

Dependency Installed:
  ncurses-devel.x86_64 0:5.9-14.20130511.el7_4                                                                                                                                                                                               

Complete!
<3>、安装zlib相关依赖
[root@jruing ~]# yum -y install zlib*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package zlib-1.2.7-17.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package zlib-devel.x86_64 0:1.2.7-17.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================================================================
 Package                                                    Arch                                                   Version                                                      Repository                                              Size
=============================================================================================================================================================================================================================================
Installing:
 zlib-devel                                                 x86_64                                                 1.2.7-17.el7                                                 centos                                                  50 k

Transaction Summary
=============================================================================================================================================================================================================================================
Install  1 Package

Total download size: 50 k
Installed size: 132 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : zlib-devel-1.2.7-17.el7.x86_64                                                                                                                                                                                            1/1 
  Verifying  : zlib-devel-1.2.7-17.el7.x86_64                                                                                                                                                                                            1/1 

Installed:
  zlib-devel.x86_64 0:1.2.7-17.el7                                                                                                                                                                                                           

Complete!
<4>、安装libxml2-devel相关依赖
[root@jruing ~]# yum install libxml2-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package libxml2-devel.x86_64 0:2.9.1-6.el7_2.3 will be installed
--> Processing Dependency: xz-devel for package: libxml2-devel-2.9.1-6.el7_2.3.x86_64
--> Running transaction check
---> Package xz-devel.x86_64 0:5.2.2-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================================================================
 Package                                                     Arch                                                 Version                                                         Repository                                            Size
=============================================================================================================================================================================================================================================
Installing:
 libxml2-devel                                               x86_64                                               2.9.1-6.el7_2.3                                                 centos                                               1.0 M
Installing for dependencies:
 xz-devel                                                    x86_64                                               5.2.2-1.el7                                                     centos                                                46 k

Transaction Summary
=============================================================================================================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 1.1 M
Installed size: 8.9 M
Is this ok [y/d/N]: y
Downloading packages:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                        5.2 MB/s | 1.1 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : xz-devel-5.2.2-1.el7.x86_64                                                                                                                                                                                               1/2 
  Installing : libxml2-devel-2.9.1-6.el7_2.3.x86_64                                                                                                                                                                                      2/2 
  Verifying  : xz-devel-5.2.2-1.el7.x86_64                                                                                                                                                                                               1/2 
  Verifying  : libxml2-devel-2.9.1-6.el7_2.3.x86_64                                                                                                                                                                                      2/2 

Installed:
  libxml2-devel.x86_64 0:2.9.1-6.el7_2.3                                                                                                                                                                                                     

Dependency Installed:
  xz-devel.x86_64 0:5.2.2-1.el7                                                                                                                                                                                                              

Complete!
<5>、安装libxslt-devel相关依赖
[root@jruing ~]# yum install libxslt-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package libxslt-devel.x86_64 0:1.1.28-5.el7 will be installed
--> Processing Dependency: libgcrypt-devel for package: libxslt-devel-1.1.28-5.el7.x86_64
--> Running transaction check
---> Package libgcrypt-devel.x86_64 0:1.5.3-14.el7 will be installed
--> Processing Dependency: libgpg-error-devel for package: libgcrypt-devel-1.5.3-14.el7.x86_64
--> Running transaction check
---> Package libgpg-error-devel.x86_64 0:1.12-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================================================================
 Package                                                          Arch                                                 Version                                                    Repository                                            Size
=============================================================================================================================================================================================================================================
Installing:
 libxslt-devel                                                    x86_64                                               1.1.28-5.el7                                               centos                                               309 k
Installing for dependencies:
 libgcrypt-devel                                                  x86_64                                               1.5.3-14.el7                                               centos                                               129 k
 libgpg-error-devel                                               x86_64                                               1.12-3.el7                                                 centos                                                16 k

Transaction Summary
=============================================================================================================================================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 453 k
Installed size: 2.6 M
Is this ok [y/d/N]: y
Downloading packages:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                        6.5 MB/s | 453 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libgpg-error-devel-1.12-3.el7.x86_64                                                                                                                                                                                      1/3 
  Installing : libgcrypt-devel-1.5.3-14.el7.x86_64                                                                                                                                                                                       2/3 
  Installing : libxslt-devel-1.1.28-5.el7.x86_64                                                                                                                                                                                         3/3 
  Verifying  : libgpg-error-devel-1.12-3.el7.x86_64                                                                                                                                                                                      1/3 
  Verifying  : libxslt-devel-1.1.28-5.el7.x86_64                                                                                                                                                                                         2/3 
  Verifying  : libgcrypt-devel-1.5.3-14.el7.x86_64                                                                                                                                                                                       3/3 

Installed:
  libxslt-devel.x86_64 0:1.1.28-5.el7                                                                                                                                                                                                        

Dependency Installed:
  libgcrypt-devel.x86_64 0:1.5.3-14.el7                                                                                libgpg-error-devel.x86_64 0:1.12-3.el7                                                                               

Complete!

<6>、安装libxslt-devel相关依赖

[root@jruing ~]# yum install python-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package python-devel.x86_64 0:2.7.5-68.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================================================================
 Package                                                     Arch                                                  Version                                                       Repository                                             Size
=============================================================================================================================================================================================================================================
Installing:
 python-devel                                                x86_64                                                2.7.5-68.el7                                                  centos                                                397 k

Transaction Summary
=============================================================================================================================================================================================================================================
Install  1 Package

Total download size: 397 k
Installed size: 1.1 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python-devel-2.7.5-68.el7.x86_64                                                                                                                                                                                          1/1 
  Verifying  : python-devel-2.7.5-68.el7.x86_64                                                                                                                                                                                          1/1 

Installed:
  python-devel.x86_64 0:2.7.5-68.el7                                                                                                                                                                                                         

Complete!

一、切换到普通用户,编译安装postgres

1、下载

https://ftp.postgresql.org/pub/source/v13.2/postgresql-13.2.tar.gz

2、部署

(1)、解压源码包并编译安装

[postgres@jruing ~]$ tar -zxvf $HOME/software/postgresql-13.2.tar.gz -C $HOME/software/
[postgres@jruing ~]$ cd $HOME/software/postgresql-13.2
[postgres@jruing ~]$ ./configure --prefix=/data/postgres/postgres-13.2 --with-python --with-libxml --with-libxslt
[postgres@jruing ~]$ make
[postgres@jruing ~]$ make install

(2)、配置环境变量

[postgres@jruing ~]$ vi $HOME/.bashrc

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

export PATH=/data/postgres/postgres-13.2/bin:$PATH
export PGHOME=/data/postgres/postgres-13.2
export PGDATA=/data/postgres/postgres-13.2/data/
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGHOME/lib/
export PATH=$PGHOME/bin:$PATH:$HOME/bin
[postgres@jruing ~]$ source $HOME/.bashrc

(3)、初始化数据库

[postgres@jruing ~]$ /data/postgres/postgres-13.2/bin/initdb -D $PGDATA -E UTF8
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 text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /data/postgres/postgres-13.2/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 ... PRC
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:

    /data/postgres/postgres-13.2/bin/pg_ctl -D /data/postgres/postgres-13.2/data/ -l logfile start

(4)、修改通用配置文件

[postgres@jruing ~]$ vi /data/postgres/postgres-13.2/data/postgresql.conf
#listen_addresses = 'localhost'         # what IP address(es) to listen on;
listen_addresses = '*'         # what IP address(es) to listen on;

#port = 5432                            # (change requires restart)
port = 10001                            # (change requires restart)

#max_connections = 100                   # (change requires restart)
max_connections = 500                   # (change requires restart)

#shared_preload_libraries = '' # (change requires restart)
shared_preload_libraries = 'pg_stat_statements' # (change requires restart)

#track_io_timing = off
track_io_timing = on

(5)、修改网桥配置文件

[postgres@jruing ~]$ vi /data/postgres/postgres-13.2/data/pg_hba.conf
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    all             all             0.0.0.0/0               md5

(6)、编译安装pg_stat_statements模块

[postgres@jruing ~]$ cd $HOME/software/postgresql-13.2/contrib/pg_stat_statements
[postgres@jruing ~]$ make
[postgres@jruing ~]$ make install

(7)、启动postgres服务

[postgres@jruing ~]$ /data/postgres/postgres-13.2/bin/pg_ctl -l /data/postgres/postgres-13.2/logs/server.log start

(8)、登陆postgres用户,创建jruing实例

[postgres@jruing ~]$ psql postgres -p 10001
postgres=# CREATE USER jruing WITH PASSWORD '123456';
CREATE ROLE
postgres=# CREATE DATABASE jruing_db OWNER jruing;
CREATE DATABASE
postgres=# exit

(9)、创建服务启停脚本

[postgres@jruing ~]$ vi $HOME/postgres-13.2_start.sh
#!/bin/bash

/data/postgres/postgres-13.2/bin/pg_ctl -l /data/postgres/postgres-13.2/logs/server.log start

[postgres@jruing ~]$ vi $HOME/postgres-13.2_stop.sh
#!/bin/bash

/data/postgres/postgres-13.2/bin/pg_ctl -l /data/postgres/postgres-13.2/logs/server.log stop

二、创建表空间和初始化创建数据库

1、创建表空间在服务器上的目录

[postgres@jruing ~]$ mkdir -p /data/postgres/postgres-13.2/tablespace/jruing_db

2、创建并查看表空间情况

[postgres@jruing ~]$ psql postgres -p 10001
postgres=# select * from pg_tablespace;
 oid  |  spcname   | spcowner | spcacl | spcoptions 
------+------------+----------+--------+------------
 1663 | pg_default |       10 |        | 
 1664 | pg_global  |       10 |        | 
(2 rows)
postgres=# CREATE TABLESPACE jruing_sche OWNER jruing LOCATION '/data/postgres/postgres-13.2/tablespace/jruing_db';
postgres=# select * from pg_tablespace;
  oid  |    spcname     | spcowner | spcacl | spcoptions 
-------+----------------+----------+--------+------------
  1663 | pg_default     |       10 |        | 
  1664 | pg_global      |       10 |        | 
 16386 | jruing_db      |    16384 |        | 
(3 rows)

3、创建模式

[postgres@jruing ~]$ psql jruing -p 10001
jruing=# create schema jruing_sche authorization  jruing_db;
jruing=# grant SELECT on jruing_sche.abc to jruing_db ;
GRANT
jruing=# grant USAGE on SCHEMA jruing_sche to jruing_db;
GRANT
posted @ 2023-03-27 15:38  Jruing  阅读(188)  评论(0编辑  收藏  举报