pg12 installation and deployment

One, rpm package installation and deployment

1. Install the RPM package

# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# yum install -y postgresql12-server

# rpm -qa | grep postgres
postgresql12-12.4-1PGDG.rhel7.x86_64
postgresql12-libs-12.4-1PGDG.rhel7.x86_64
postgresql12-server-12.4-1PGDG.rhel7.x86_64

2. Add users

groupadd postgres
useradd -g postgres postgres

3. Configuration data and log directory

# mkdir -p /data/pgsql/{data,logs}
# chown -R postgres:postgres /data/pgsql

4. Initialize the database

# su - postgres
su: warning: cannot change directory to /home/postgres: No such file or directory
-bash-4.2$
-bash-4.2$  /usr/pgsql-12/bin/initdb -E utf8 -D /data/pgsql/data/
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/pgsql/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/pgsql-12/bin/pg_ctl -D /data/pgsql/data/ -l logfile start


5. Start the database

-bash-4.2$ /usr/pgsql-12/bin/postgres -D /data/pgsql/data/ > /data/pgsql/logs/postgres.log &
[1] 18534

6. Create users and databases

-bash-4.2$ /usr/pgsql-12/bin/psql
psql (12.4)
Type "help" for help.
postgres=# create user sansi with password '123';
CREATE ROLE
postgres=# create database db1 with encoding='utf8' owner='sansi';
CREATE DATABASE

#验证登录
-bash-4.2$ /usr/pgsql-12/bin/psql -U sansi -d db1
psql (12.4)

7. Environment variable configuration

$ cat /home/postgres/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

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

export PATH
export PGHOME=/usr/local/pgsql          #软件安装目录
export PGDATA=/data/pgsql12/data        #PG数据目录

$ source /home/postgres/.bash_profile

Two, source installation

1. Install the source code package

# wget https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.bz2
# tar xf postgresql-12.2.tar.bz2 -C /usr/local/

2. Install dependent packages

# yum install gcc gcc-c++ readline-devel readline readline-dev zlib-devel

3. Compile and install

# cd /usr/local/postgresql-12.2/
# ./configure --prefix=/usr/local/pgsql
# make && make install

4. Add users

groupadd postgres
useradd -g postgres postgres

5. Configuration data, log directory

# mkdir -p /data/pgsql12/{data,logs}
# chown -R postgres:postgres /data/pgsql12/

6. Initialize the database

# su - postgres
Last login: Tue Sep  1 22:52:40 CST 2020 on pts/0
[postgres@sansi_test ~]$ /usr/local/pgsql/
bin/     include/ lib/     share/

[postgres@sansi_test ~]$ /usr/local/pgsql/bin/initdb -D /data/pgsql12/data/
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.

fixing permissions on existing directory /data/pgsql12/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/pgsql/bin/pg_ctl -D /data/pgsql12/data/ -l logfile start

7, start the database

[postgres@sansi_test data]$ /usr/local/pgsql/bin/pg_ctl -D /data/pgsql12/data/ -l logfile start
waiting for server to start.... done
server started

8. Create users and related databases

When the database is initialized, a user with the same name as the operating system will be created as a super user in the database. Therefore, you do not need to use an account and password to log in to the database under the OS user. The super user login is used by default. Of course, you can also modify pg_hba.conf. The configuration file is controlled.

[postgres@sansi_test data]$ /usr/local/pgsql/bin/createdb db1
[postgres@sansi_test data]$ /usr/local/pgsql/bin/createuser sansi
[postgres@sansi_test data]$ /usr/local/pgsql/bin/psql -U sansi -d db1
psql (12.2)
Type "help" for help.

db1=>

9. Environment variable configuration

$ cat /home/postgres/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

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

export PATH
export PGHOME=/usr/local/pgsql          #软件安装目录
export PGDATA=/data/pgsql12/data        #PG数据目录


$ source /home/postgres/.bash_profile

Three, the three modes of PG database shutdown (smart/fast/immediate)

grammar

pg_ctl stop -D ${datadir} -m smart/fast/immediate

1、 Smart Shutdown

After receiving the shutdown command, the server will no longer accept new link requests and wait for the currently established connection to end.

If the database is in online backup mode, you need to wait until the online backup mode is no longer active before shutting down. At this time, the connection of the super user is allowed, mainly for the connection of the super user to terminate the connection backup mode.

If the database is in the recovery state, it will wait for the recovery and streaming replication session to terminate before closing.

This mode is more inclined to shut down the database server before maintenance, and it is generally used less, because in this mode, the user needs to actively disconnect the connection before starting to close the database.

 /usr/pgsql-12/bin/pg_ctl  stop -D /data/pgsql/data/ -m smart
waiting for server to shut down.... done
server stopped

2、Fast Shutdown

The default shutdown mode, fast shutdown mode. The main process postmaster does not allow new connections and sends all existing service processes to send a SIGINT signal to let them abort the current transaction and exit immediately. Close the database after waiting for all service processes to exit. If the server is in online backup mode, the backup mode will terminate, thereby invalidating the backup.

SIGINT "Fast" mode will not wait for the client to disconnect and will terminate the ongoing online backup. All active transactions are rolled back and the client is forcibly disconnected, and then the server is shut down.

3、Immediate Shutdown

This is the immediate shutdown mode. The main process postmaster sends SIGQUIT to all child processes and exits immediately. All child processes will also exit immediately without performing normal database shutdown processing. This will result in recovery (by replaying the WAL log) at the next startup. It is recommended to use it only in emergency situations.

Guess you like

Origin blog.csdn.net/weixin_37692493/article/details/108500373