PostgreSQL-源码离线编译安装

一、官网下载源码包,本文以postgresql 11.22为例

PostgreSQL: File Browserhttps://www.postgresql.org/ftp/source/v11.22/
二、在服务器上安装所需pgsql所需依赖

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

纯内网服务器,无法连接外网情况,在可联网的虚拟机或者公网服务器上操作

yum install yum-utils
yumdownloader --resolve --destdir=/home/packages/ perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake

将以上依赖从虚拟机或者公网服务器拉入内网机器

三、开始进行pgsql源码编译

1、创建文件夹,将pgsql压缩包移入

[root@localhost /]# mkdir /soft
[root@localhost /]# mv postgresql-11.22.tar.gz  /soft/
[root@localhost /]# cd /soft/
[root@localhost soft]# ls
postgresql-11.22.tar.gz

2、解压压缩包

[root@localhost soft]#tar -zxvf postgresql-1122.tar.gz


[root@localhost soft]# cd postgresql-11.22
[root@localhost postgresql-11.22]# ls
aclocal.m4  config  configure  configure.ac  contrib  COPYRIGHT  doc  GNUmakefile.in  HISTORY  INSTALL  Makefile  README  src

3、创建pgsql目录,编译源码

[root@localhost postgresql-11.22]#mkdir -p /opt/pgsql/postgresql
[root@localhost postgresql-11.22]#./configure --prefix=/opt/pgsql/postgresql
###注意事项: –prefix=prefix 安装到prefix指向的目录;默认为/usr/local/pgsql

源码编译:
当前路径执行make
[root@localhost postgresql-11.22]#make
等待编译完成
完成postgreql的安装。进入/pgsql/postgresql目录看安装后的postgresql的文件

[root@localhost postgresql-11.22]#cd /opt/pgsql/postgresql/
[root@localhost postgresql/]#pwd
/opt/pgsql/postgresql/
[root@localhost postgresql/]#ls

创建用户和用户组
创建用户组postgres、用户postgres
#Linux执行如下语句11:

[root@localhost postgresql]# groupadd postgres
[root@localhost postgresql]# useradd -g postgres postgres
[root@localhost postgresql]# id postgres
uid=1000(postgres) gid=1000(postgres) groups=1000(postgres)

4、创建数据主目录


#创建postgresql数据库的数据主目录并修改文件所有者
数据库主目录是随实际情况而不同,这里的主目录是在/pgsql/postgresql/data目录下
#Linux执行如下语句12:
[root@localhost postgresql]# cd /opt/pgsql/postgresql
[root@localhost postgresql]#  mkdir data
[root@localhost postgresql]# chown postgres:postgres data
[root@localhost postgresql]# ls -la
total 16
drwxr-xr-x 7 root     root       68 Dec 13 14:53 .
drwxr-xr-x 3 root     root       24 Dec 13 14:40 ..
drwxr-xr-x 2 root     root     4096 Dec 13 14:52 bin
drwxr-xr-x 2 postgres postgres    6 Dec 13 14:53 data
drwxr-xr-x 6 root     root     4096 Dec 13 14:52 include
drwxr-xr-x 4 root     root     4096 Dec 13 14:52 lib
drwxr-xr-x 6 root     root     4096 Dec 13 14:52 share

5、配置环境变量

进入home/postgres目录可以看到.bash_profile文件。
#Linux执行如下语句13:
[root@localhost postgresql]# cd /home/postgres/
[root@localhost postgres]# ls -la
total 12
drwx------  2 postgres postgres  62 Dec 13 14:53 .
drwxr-xr-x. 3 root     root      22 Dec 13 14:53 ..
-rw-r--r--  1 postgres postgres  18 Apr 11  2018 .bash_logout
-rw-r--r--  1 postgres postgres 193 Apr 11  2018 .bash_profile
-rw-r--r--  1 postgres postgres 231 Apr 11  2018 .bashrc

6、修改.bash_profile文件

[root@localhost postgres]# vi .bash_profile
[root@localhost postgres]# cat .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
export PATH

export PGHOME=/opt/pgsql/postgresql
export PGDATA=$PGHOME/data
export PATH=$PATH:$HOME/bin:$PGHOME/bin
保存,退出vi。执行以下命令,使环境变量生效
[root@localhost postgres]# source .bash_profile

7、初始化数据库

切换用户到postgres并使用initdb初使化数据库
[root@localhost postgres]# su - postgres

[postgres@localhost ~]$ initdb

查看/pgsql/postgresql/data目录,看是否初始化成功

[postgres@localhost ~]$ ls -lh $PGDATA

8、配置服务

修改/opt/pgsql/postgresql/data目录下postgresql.conf文件,该文件配置PostgreSQL数据库服务器的相应的参数。
[postgres@localhost ~]$ cd /opt/pgsql/postgresql/data
 
[postgres@localhost data]$ vi postgresql.conf
[postgres@localhost data]$ cat  postgresql.conf

注意:其中,参数“listen_addresses”表示监听的IP地址,默认是在localhost处监听,也就是127.0.0.1的ip地址上监听,只接受来自本机localhost的连接请求,这会让远程的主机无法登陆这台数据库,如果想从其他的机器上登陆这台数据库,需要把监听地址改为实际网络的地址,一种简单的方法是,将行开头的#去掉,把这个地址改为*,表示在本地的所有地址上监听。
修改/opt/pgsql/postgresql/data目录下pg_hba.conf 文件,该文件 配置对数据库的访问权限。
[postgres@localhost data]$ vi pg_hba.conf
[postgres@localhost data]$ cat  pg_hba.conf
...
# 添加下面这一行 
# IPv4 local connections:
host    all             all             0.0.0.0/0               trust
host    all             all             127.0.0.1/32            trust
设置开机自启动
PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。linux文件即为linux系统上的启动脚本。需切换为root用户。
#Linux执行如下语句20:
[postgres@localhost start-scripts]$ su root
[root@localhost data]# cd /soft/postgresql-14.6/contrib/start-scripts
[root@localhost start-scripts]# ls
freebsd  linux  macos
切换为root用户,修改linux文件属性,添加X属性

[root@localhost start-scripts]# chmod a+x linux
复制linux文件到/etc/init.d目录下,更名为postgresql

[root@localhost start-scripts]# cp linux /etc/init.d/postgresql
修改/etc/init.d/postgresql文件的两个变量

[root@localhost start-scripts]# vi /etc/init.d/postgresql
[root@localhost start-scripts]# cat  /etc/init.d/postgresql
...
## EDIT FROM HERE
# Installation prefix
#prefix=/usr/local/pgsql
prefix=/opt/pgsql/postgresql
# Data directory
#PGDATA="/usr/local/pgsql/data"
PGDATA="/opt/pgsql/postgresql/data"
设置postgresql服务开机自启动

[root@localhost start-scripts]# chkconfig --add postgresql
[root@localhost start-scripts]# chkconfig

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
postgresql      0:off   1:off   2:on    3:on    4:on    5:on    6:off

9、启动数据库服务

启动PostgreSQL服务
[root@localhost start-scripts]# service postgresql start
Starting PostgreSQL: ok
 
查看PostgreSQL服务,确认是否启动成功
[root@localhost start-scripts]# ps -ef | grep postgres
root     20099  9787  0 14:55 pts/0    00:00:00 su - postgres
postgres 20100 20099  0 14:55 pts/0    00:00:00 -bash
postgres 20424     1  0 15:13 ?        00:00:00 /opt/pgsql/postgresql/bin/postmaster -D /opt/pgsql/postgresql/data
postgres 20426 20424  0 15:13 ?        00:00:00 postgres: checkpointer
postgres 20427 20424  0 15:13 ?        00:00:00 postgres: background writer
postgres 20428 20424  0 15:13 ?        00:00:00 postgres: walwriter
postgres 20429 20424  0 15:13 ?        00:00:00 postgres: autovacuum launcher
postgres 20430 20424  0 15:13 ?        00:00:00 postgres: stats collector
postgres 20431 20424  0 15:13 ?        00:00:00 postgres: logical replication launcher
[root@localhost start-scripts]# netstat -ltnup | grep post
测试本地连接
切换设置的postgres用户

[root@localhost start-scripts]# su - postgres
Last login: Wed Dec 13 15:13:04 CST 2023 on pts/0
[postgres@localhost ~]$  psql

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值