PostgreSQL source code compilation and installation detailed tutorial (linux environment)


foreword

This article uses make source code to compile and install PostgreSQL. Although compared with the yum installation method, this method is slightly more complicated, but in order to facilitate management and maintenance (for example: to specify the database installation directory, modify parameters, install extensions, etc.), the following details the method Installation method.

1. Download the specified version of the source code installation package

Source code installation package download address: http://www.postgresql.org/ftp/source/
as shown below, this article takes downloading version 14.7 as an example
insert image description here

2. Install dependency packages

1. Installation in a networked environment

yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake

2. Offline environment installation

In the networked environment, download the package in the following way, and then copy it to the offline environment.
Offline dependency package download address: https://pkgs.org/

# 使用yumdownloader下载
# 安装yum-utils包,内部包含yumdownloader等命令
yum install yum-utils
# 例如下载docker.x86_64包
yumdownloader --resolve --destdir=/pack docker.x86_64
# 离线环境使用下面命令安装包
rpm -ivh  *.rpm  --nodeps  --force

3. Install the PostgreSQL database

1. Create an installation directory and unzip the installation package

# 创建安装目录
mkdir -p /pgsql/postgresql
# 上传至安装目录并解压安装包
tar -zxvf postgresql-14.7.tar.gz

2. Configure PostgreSQL source code

# 进入解压后的文件夹
cd postgresql-14.7
# 配置源码
./configure --prefix=/pgsql/postgresql

In this process, edit parameters can be specified as required, for example, the parameter description is as follows:

–prefix=prefix: install to the directory pointed to by prefix; the default is /usr/local/pgsql
–bindir=dir: install the application to dir; the default is prefix/bin
–with-docdir=dir: install the document to dir; the default is prefix/doc
–with-pgport=port: Set the default server-side network connection service TCP port number
–with-tcl: Provide Tcl stored procedure support for the server
–with-perl: Provide Perl stored procedure support for the server
–with- python: Provide Python stored procedure support for the server
3. Edit and install

# 确保进入解压安装包的目录
cd /pgsql/postgresql/postgresql-14.7
# 编辑安装
make && make install

4. Create users, groups, and data directories.
The main directory of the database can vary with the actual situation. Here our main directory is in the /pgsql/postgresql/data directory.

# 创建组
groupadd postgres
# 创建用户
useradd -g postgres postgres
# 创建数据目录
mkdir –p /pgsql/postgresql/data
# 修改目录归属
chown postgres:postgres /pgsql/postgresql/data

5. Configure the environment variable
root user to perform the following operations

# 编辑修改.bash_profile文件
vi /home/postgres/.bash_profile

Add the following content at the end of the file:
export PGHOME=/pgsql/postgresql
export PGDATA=/pgsql/postgresql/data
PATH= PATH : PATH:PATH:HOME/bin:$PGHOME/bin

# 使修改的.bash_profile文件立即生效
source /home/postgres/.bash_profile

6. Initialize the database.
The following message appears, indicating that the installation is complete.

# 切换至postgres用户
 su - postgres
# 使用initdb命令初始化数据库
initdb

insert image description here

3. Configuration service

In order to make the database more convenient to use, you can modify two files in the /pgsql/postgresql/data directory:
postgresql.conf: configure the corresponding parameters of the PostgreSQL database server.
pg_hba.conf : Configure access to the database.

vi /pgsql/postgresql/data/postgresql.conf 

Modifications:

listen_addresses = '*' 

Among them, the parameter "listen_addresses" indicates the listening IP address. The default is to listen on the localhost, that is, to listen on the ip address of 127.0.0.1, and only accept connection requests from the localhost of the machine, which will prevent the remote host from logging in. Database, if you want to log in to this database from other machines, you need to change the listening address to the actual network address. A simple method is to remove the # at the beginning of the line and change the address to * to indicate that it is in the local Listen on all addresses.

vi /pgsql/postgresql/data/pg_hba.conf
# 以下第一条为增加内容
host    all             all             0.0.0.0/0               trust
host    all             all             127.0.0.1/32            trust

After adding the first item, people in the local area network can access it

4. Set PostgreSQL to start automatically at boot

1. Modify the startup script
PostgreSQL's startup script is located in the contrib/start-scripts path of the PostgreSQL source code directory.
The file named linux is the startup script on the linux system.

# copt至系统的/etc/init.d/目录并命名为postgresql
cp /pgsql/postgresql-14.7/contrib/start-scripts /etc/init.d/postgresql
# 修改/etc/init.d/postgresql文件的两个变量
vi /etc/init.d/postgresql

Modify content:
prefix is ​​set to the installation path of postgresql: /pgsql/postgresql
PGDATA is set to the data directory path of postgresql: /pgsql/postgresql/data
insert image description here

2. Set the automatic

# 设置postgresql服务开机自启动
chkconfig --add postgresql

5. Open the port and start the database

# 开放5432端口
irewall-cmd --zone=public --add-port=5432/tcp --permanent
# 配置立即生效
firewall-cmd --reload
# 启动数据库
pg_ctl start

View the postgresql process, the following information appears, indicating that the startup is successful

ps -ef | grep postgres

insert image description here

Summarize

Refer to this article to ensure that the installation program, path and configuration information can be modified according to your needs.

Guess you like

Origin blog.csdn.net/ma286388309/article/details/129010124#comments_28309352