PG安装部署

太阳2年前技术文章709

一、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.


标签: PostgreSQL

相关文章

REPMGR-PG高可用搭建(一)

REPMGR-PG高可用搭建(一)

PG高可用对比数据库复制的术语和定义这些术语和定义应该有助于讨论复制。在与其他Postgres开发人员进行了大量讨论之后,我编译了它们,但是这些定义应该是普遍可用的,并且也应该适用于其他RDBMS。复...

PG参数整理

一、参数的分类参数的类型名称说明internal内部参数,只读无法修改。postgres程序写死或者是在初始化指定后无法修改的参数postmaster更改该类参数,需重启生效sighup不需重启,重新...

PG的统计信息(三)

1.3 数据分布类统计信息1.3.1 pg_stats通过对pg_stats的查询,可以查看每个字段的数据分析统计信息,类似SQL Server的直方图,为优化器选择最佳执行计划提供依据,pg_sta...

PG常用命令

1、连库相关#连库 $ psql -h <hostname or ip> -p <端口> [数据库名称] [用户名称] #连库并执行命令 $ psql -h <ho...

PG体系结构(一)

PG体系结构(一)

一、进程结构PG数据库启动时会先启动一个主进程(9.3之前称为postmaster,9.3以后称为postgres server process),然后fork出一些辅助子进程(backend、bac...

PG的执行计划

一、Explain基本使用1.1 命令解释explain [ ( option [,...] ) ] statement explain [ analyze ] [ verbose ] statem...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。