(转)postgreSQL 12 源码安装

 

原文:https://blog.csdn.net/qq1130207965/article/details/103399540

export PATH=/approot/pgsql/bin:$PATH
export LD_LIBRARY_PATH=/approot/pgsql/lib:$LD_LIBRARY_PATH
export PGDATA=/approot/osdb/pgdata
export PGHOST=/tmp

原文:https://www.cnblogs.com/halberd-lee/p/11699694.html

1 下载
官网提供了源码和预安装的版本。 源码需要编译安装,解决依赖包等问题,而预安装的版本要简单很多。只需下载解压, 初始化数据库即可。

本例以源码安装为例:请至官网 下载源码。

本例中安装的是PG12. 在 https://www.postgressql.org/ftp/source/v12.0/ 中选择postgressql-12.0.tar.bz2

请根据需要先择自己需要的版本。

预安装版本,请至官网下载。

2 准备环境
此步骤以root用户执行。 将postgressql的安装包放到/approot/tools/ 路径。

# 创建组和用户
groupadd postgres
useradd -g postgres -G postgres -d /home/postgresql postgres
passwd postgres
# 安装依赖包
yum install -y readline-devel zlib-devel
# 将安装包放在/opt 路径中并解压
cd /approot/tools/
tar -xvf postgresql-12.6.tar.gz
3 编译安装
此步骤以postgres 用户操作:

cd /approot/tools/postgresql-12.6
./configure --prefix=/approot/pgsql12.6 --with-perl --with-python
---报错---
https://blog.csdn.net/qq_33714590/article/details/64437734
configure: error: could not determine flags for linking embedded Perl.
解决方法:
yum install perl-ExtUtils-Embed

make && make install
cd /approot/
ln -sf pgsql12.6/ pgsql

安装后,可执行文件,库文件等都会安装在/approot/pgsql 中。

4 设置环境变量
将以下两行添加至postgres 用户的环境变量文件 .bash_profile 并生效。

export PATH=/approot/pgsql/bin:$PATH
export LD_LIBRARY_PATH=/approot/pgsql/lib:$LD_LIBRARY_PATH
export PGDATA=/approot/osdb/pgdata
export PGHOST=/tmp

环境变量文件生效方法:

. .bash_profile
或者
source .bash_profile
5 初始化数据库
此步骤以postgres用户执行。

创建数据存放路径

mkdir /approot/osdb/pgdata
postgressql 数据库的配置文件,数据文件等都会存放在这个路径下。

初始化数据库

initdb --locale=C -E UNICODE -D /approot/osdb/pgdata
示例如下:

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 "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /home/postgressql/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 ... America/New_York
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 /home/postgressql/data/ -l logfile start
初始化完成后,在最后一行会提示我们启动数据库的方法. 此时我们来查看一下 $HOME/data路径 的内容:

[postgres@boss20 pgdata]$ ls
base pg_commit_ts pg_hba.conf pg_logical pg_notify pg_serial pg_stat pg_subtrans pg_twophase pg_wal postgressql.auto.conf
global pg_dynshmem pg_ident.conf pg_multixact pg_replslot pg_snapshots pg_stat_tmp pg_tblspc PG_VERSION pg_xact postgressql.conf
这些路径分别有什么用途,以后再了解。

安装contrib目录下的工具
contrib下有一些工具比较实用, 一般用户都会安装这些工具, 其安
装的方法也与Linux下的编译过程相同, 安装命令如下:
cd postgresql-12.2/contrib
make
sudo make install

6 配置参数文件
我们主要是配置postgressql.conf 和 pg_hba.conf 这两个。

postgressql.conf 针对实例的配置
pg_hba.conf 针对数据库访问的控制
6.1 postgressql.conf
找到 #port 和 #listener_address 这两个参数。这两个参数是相邻的:

#listen_addresses = 'localhost' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
#port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
将两行行首的 # 删除,将 localhost 改为当前服务器的IP 。 默认的监听端口是5432,可以自行指定另外一个端口号,*不建议使用默认端口号* 。 顺便再修改一下 max_connections 吧,最大的连接数, 100有点少,特别是业务系统。 可以调整成1000,或者更高。

6.2 pg_hba.conf
将以下一行添加至文末。

host all all 10.10.100.0/24 trust
其意义是允许 10.10.100网段的IP 连接此服务器上的PG. 如果想允许所有IP 都可以连接此服务器 则可以配置成如下:

host all all 0.0.0.0/0 trust
7 数据库启动与关闭

7.1 手动
我们手动启动与关闭数据库是执行pg_ctl命令。在执行时,需要指定 数据路径,格式如下:

pg_ctl -D <数据存放路径> [ stop | start ]
示例如下:

[postgres@boss20 data]$ pg_ctl -D /home/postgressql/data/ -l logfile start
waiting for server to start.... done
server started
[postgres@boss20 data]$ pg_ctl -D $HOME/data stop
waiting for server to shut down.... done
server stopped
7.2 开机自动启动
此步骤需要root用户操作。 postgressql 的安装包中提供了数据库启动与关闭的脚本,可以帮助我们简化操作,也可以 用作开机启动的脚本和service/systemctl 控制服务的脚本。 脚本位于:

<path>/postgressql-12.0/contrib/start-scripts/
本示例中是:
/opt/postgressql-12.0/contrib/start-scripts/
设置开机启动:

cp /opt/postgressql-12.0/contrib/start-scripts/linux /etc/init.d/postgressql
chkconfig --add postgressql
chmod 755 /etc/init.d/postgressql
调整配置,主要是脚本中三个变量的值:

prefix="/home/postgressql/dbhome"
PGDATA="/home/postgressql/data"
PGUSER=postgres
prefix 是软件的安装路径
PGDATA 是数据存放路径
PGUSER 是启动postgressql服务的用户
以后可以开机启动和通过service 命令控制启动和关闭了:

[root@boss20 init.d]# service postgressql start
Starting postgresSQL: ok
[root@boss20 init.d]# service postgressql stop
Stopping postgresSQL: ok

posted @ 2021-03-08 10:02  liujiacai  阅读(308)  评论(0编辑  收藏  举报