作者:太阳
PostgreSQL是一款开源的高度可扩展、跨平台的关系型数据库管理系统,支持复杂数据类型、ACID事务、触发器、存储过程等特性,具备强大的查询优化器和完整性约束,拥有庞大的全球社区支持。它适用于各种规模和类型的应用,提供稳定可靠的数据库解决方案。下面将分别介绍 PostgreSQL的rpm包安装和源码安装部署的详细步骤。

一、rpm包安装部署

1、安装RPM包

# 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、添加用户

# groupadd postgres
# useradd -g postgres postgres

3、配置数据、日志目录

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

4、初始化数据库

# su - postgres
$
$  /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、启动数据库

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

6、创建用户和数据库

$ /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

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

7、环境变量配置

$ 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

二、源码安装

1、安装源码软件包

# yum install lbzip2
# 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、安装依赖包

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

3、编译安装

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

编译时一些必要的参数:

  • -with-perl : 编译时添加该参数才能够使用perl语法的PL/Perl过程语言写自定义函数,一般都需要。需要提前安装好相关的perl开发包:libperl-dev
  • -with-python : 编译时添加该参数才能够使用python语法的PL/Perl过程语言写自定义函数,一般都需要。需要提前安装好相关的python开发包:python-dev
  • -with-blocksize=128 --with-wal-blocksize=128 : 默认情况下PG数据库的数据页大小为8KB,若数据库用来做数仓业务,可在编译时将数据页进行调整,以提高磁盘IO
  • -enable-thread-safety : 保证客户端线程安全,该参数在PG 9.0以上默认线程安全,如果安装的是8.0版本需要手动指定该参数

4、添加用户

# groupadd postgres
# useradd -g postgres postgres

5、配置数据、日志目录

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

6、配置环境变量

# vi /etc/profile
export PGHOME=/usr/local/pgsql
export PGDATA=/data/pgsql12/data
export PATH=$PGHOME/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/pgsql/lib

# source /etc/profile

7、初始化数据库

# su - postgres
Last login: Tue Sep  1 22:52:40 CST 2020 on pts/0

$ 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:

    pg_ctl -D /data/pgsql12/data/ -l logfile start

8、启动数据库

$ pg_ctl -D /data/pgsql12/data/ -l logfile start
waiting for server to start.... done
server started

9、验证登录并修改用户验证方式

在对数据库做初始化时会创建一个与操作系统同名的用户在数据库的超级用户,所以在改OS用户下登录数据库默认不需要使用账户和密码,当然也可以通过修改 pg_hba.conf 配置文件进行控制。

## 通过postgres用户免密登录数据库,并修改该数据库用户密码
$ psql
psql (12.2)
Type "help" for help.
postgres=# alter user postgres password '123';
ALTER ROLE
postgres=# \q
## 修改认证配置文件,要求全部进行md5认证(将trust改为md5)
local   all             all                                     md5
host    all             all             127.0.0.1/32            md5
## 修改配置文件后reload重新载入配置文件
$ pg_ctl reload
## 再次验证是否必须密码登录
$ psql
Password for user postgres:                 //输入密码
psql (12.2)
Type "help" for help.

更多技术信息请查看云掣官网https://yunche.pro/?t=yrgw


云掣科技
40 声望12 粉丝

专注于云msp服务