源码编译安装PostgreSQL以及多实例

一、源码编译安装PostgreSQL

1、CentOS7安装所需准备:

1、源码包地址:
https://ftp.postgresql.org/pub/source/v14.2/postgresql-14.2.tar.gz
2、所需的依赖包:
yum install -y gcc make readline-devel zlib-devel

2、源码编译安装

1、解压缩至/usr/local中
tar xf postgresql-14.2.tar.gz -C /usr/local
2、进入对应的编译目录
cd  /usr/local/postgresql-14.2/
3、进行编译
./configure --prefix=/apps/pgsql     #指定编译安装的目录
4、make -j 4 world    #world 包含文档和其他模块等,默认make不包含
5、make install-world   #install-world 包含安装文档,默认make install 不包含

3、安装参考

------------------------------------------------------------------------


Short Version

    ./configure
    make
    su
    make install
    adduser postgres
    mkdir /usr/local/pgsql/data
    chown postgres /usr/local/pgsql/data
    su - postgres
    /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
    /usr/local/pgsql/bin/createdb test
    /usr/local/pgsql/bin/psql test

The long version is the rest of this document.

------------------------------------------------------------------------


Requirements

[root@7centos7 postgresql-14.2]# less INSTALL 
[root@7centos7 postgresql-14.2]# pwd
/usr/local/postgresql-14.2

4、创建数据库的用户和组

注:PostgreSQL默认不支持以root身份启动,基于安全考虑必须创建一个普通用户用于启动PostgreSQL

1、创建普通用户(后期用于管理的管理员账户)
useradd [-s /bin/bash] [-m] [-d /home/postgres]  postgres
2、设置密码(第三个设置密码的方式只有在CentOS上可用)
echo -e '000000\n000000' | passwd postgres
echo  postgres:000000 |chpasswd 
echo '000000' | passwd  --stdin  postgres

5、创建相应目录并进行授权

1、mkdir -pv /pgsql/data
2、chown postgres. /pgsql/data

6、设置环境变量

[root@7centos7 ~]# vim   /etc/profile.d/pgsql.sh 
#!/bin/bash
export PGHOME=/apps/pgsql
export PATH=$PGHOME/bin/:$PATH
export PGDATA=/pgsql/data
export PGUSER=postgres
export MANPATH=/apps/pgsql/share/man:$MANPATH
[root@7centos7 ~]# .  /etc/profile.d/pgsql.sh

7、登录并查看帮助

[root@7centos7 postgresql-14.2]# su - postgres
[postgres@7centos7 ~]$ psql
psql (14.2)
Type "help" for help.

postgres=# help
You are using psql, the command-line interface to PostgreSQL.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

8、初始化数据库

#此处参照安装参考完整命令应该为: initdb   -D   /pgsql/data ,因为设置了环境变量,所以可以省略。
[postgres@7centos7 ~]$ initdb     
...
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 /pgsql/data -l logfile start
pg_ctl -D /pgsql/data -l logfile start   #启动服务


#建议的初始化方式:
initdb -A  md5 -D  $PGDATA  -E utf8  --locale=C  -U postgres  -W
-A		#指定locale connections默认的身份验证方法
-D		#指定数据目录
-E		#指定字符集
--locale=C	#指定语言环境
-U		#指定数据库superuser用户名
-W		#指定数据库superuser用户密码

9、启动和关闭服务

#默认端口号为5432
#启动服务
[postgres@7centos7 ~]$  pg_ctl -D /pgsql/data -l logfile start
waiting for server to start.... done
server started
[postgres@7centos7 ~]$ ss -ntl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128        *:22                     *:*                  
LISTEN     0      128    127.0.0.1:631                    *:*                  
LISTEN     0      128    127.0.0.1:5432                   *:*                  
...
pg_ctl -D /pgsql/data -l logfile start|stop  <==> pg_ctl   start|stop
#-l logfile   日志。该选项不加的时候日志信息会在屏幕上打印。

10、为维护方便,写一个service文件用来管理

[root@7centos7 postgresql-14.2]# vim /lib/systemd/system/postgresql.service
[Unit]
Description=PostgreSQL databases server
After=net.work.target

[Service]
User=postgres
Group=postgres

ExecStart=/apps/pgsql/bin/postmaster -D /pgsql/data
ExecReload=/bin/kill -HUP

[Install]
WantedBy=multi-user.targer                                                                         

[root@7centos7 postgresql-14.2]# systemctl daemon-reload 
[root@7centos7 postgresql-14.2]# systemctl start postgresql.service 
[root@7centos7 postgresql-14.2]# ss -ntl
State       Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN      0      128                   *:22                                *:*                  
LISTEN      0      128           127.0.0.1:631                               *:*                  
LISTEN      0      128           127.0.0.1:5432                              *:*                  
LISTEN      0      100           127.0.0.1:25                                *:*                  
LISTEN      0      128                   *:111                               *:*                  
LISTEN      0      128                [::]:22                             [::]:*                  
LISTEN      0      128               [::1]:631                            [::]:*                  
LISTEN      0      128               [::1]:5432                           [::]:*                  
LISTEN      0      100               [::1]:25                             [::]:*                  
LISTEN      0      128                [::]:111                            [::]:*                  
[root@7centos7 postgresql-14.2]# systemctl status postgresql.service 
● postgresql.service - PostgreSQL databases server
   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
   Active: active (running) since 四 2022-11-10 20:49:34 CST; 7s ago
 Main PID: 21741 (postmaster)

注:让某个服务开机自启的办法:

1、把某个命令写入/etc/rc.local文件中。/etc/rc.local 也是我经常使用的一个脚本。
该脚本是在系统初始化级别的脚本运行之后再执行的,因此可以安全地在里面添加你想在系统启动之后执行的脚本。
例:
[root@7centos7 ~]# ll  /etc/rc.local
lrwxrwxrwx 1 root root 13 10月  2 11:24 /etc/rc.local -> rc.d/rc.local    #注/etc/rc.local是链接文件,源文件是/etc/rc.d/rc.local

[root@7centos7 ~]# cat /etc/rc.local
#增加该行
su - postgres -c "/apps/pgsql/bin/pg_ctl start"   #一定要切换至postgres 用户,-c  后面接命令,一定是绝对路径

[root@7centos7 ~]# chmod +x /etc/rc.d/rc.local    #源文件要加执行权限,不然该服务不会自动启动

2、如果同时存在的话,在目录 /etc/init.d/ 下的脚本文件的优先级会高于目录 /etc/systemd/system/ 下的 service 文件。
#源码目录中内置PostgreSQL的启动脚本
/usr/local/postgresql-14.2/contrib/start-scripts/linux

[root@7centos7 start-scripts]# cp linux  /etc/init.d/postgresqld
[root@7centos7 start-scripts]# vim /etc/init.d/postgresqld 
#修改以下两行
# Installation prefix
prefix=/apps/pgsql
# Data directory
PGDATA="/pgsql/data"                                                                               
[root@7centos7 start-scripts]# chmod +x  /etc/init.d/postgresqld 
[root@7centos7 start-scripts]# chkconfig --add postgresqld
[root@7centos7 start-scripts]# 
[root@7centos7 start-scripts]# service post
postfix      postgresqld  
[root@7centos7 start-scripts]# service postgresqld start 
Starting PostgreSQL: sok
[root@7centos7 start-scripts]# ss -ntl
State       Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN      0      128                   *:22                                *:*                  
LISTEN      0      128           127.0.0.1:631                               *:*                  
LISTEN      0      128           127.0.0.1:5432                              *:*                  
3、写service文件(同上,略)

二、postgresql多实例的实现

1、先停止所有的postgresql服务
[root@7centos7 data]# service postgresqld stop  #本次实验是在编辑 /etc/init.d/postgresqld
文件下启动的服务,所以停止该服务:service postgresqld stop

2、建立相对应的目录,并修改权限
[root@7centos7 pgsql]# mkdir -pv ./data2
mkdir: 已创建目录 "./data2"
[root@7centos7 pgsql]# ll
总用量 4
drwx------ 19 postgres postgres 4096 11月 10 22:20 data
drwxr-xr-x  2 root     root        6 11月 10 22:26 data2
[root@7centos7 pgsql]# chown postgres. data2
[root@7centos7 pgsql]# ll
总用量 4
drwx------ 19 postgres postgres 4096 11月 10 22:20 data
drwxr-xr-x  2 postgres postgres    6 11月 10 22:26 data2

3、切换身份至 postgres
[root@7centos7 pgsql]# su - postgres

4、初始化数据库
[postgres@7centos7 ~]$ pg_ctl initdb -D /pgsql/data2
#查看是否生成数据
[postgres@7centos7 ~]$ ls /pgsql/data2
base          pg_hba.conf    pg_notify     pg_stat      pg_twophase  postgresql.auto.conf
global        pg_ident.conf  pg_replslot   pg_stat_tmp  PG_VERSION   postgresql.conf
pg_commit_ts  pg_logical     pg_serial     pg_subtrans  pg_wal
pg_dynshmem   pg_multixact   pg_snapshots  pg_tblspc    pg_xact

5、开启服务
[postgres@7centos7 ~]$  /apps/pgsql/bin/pg_ctl -D /pgsql/data2 -l logfile start
waiting for server to start.... stopped waiting
pg_ctl: could not start server
Examine the log output.
#启动失败,由于之前开启了服务,且该服务的端口也是5432。

6、解决办法:

 (1). 修改/pgsql/data2的端口
 (2). 停掉之前的/pgsql/data的服务,然后再开启/pgsql/data2对应的服务

(1)
[postgres@7centos7 ~]$ vim /pgsql/data2/postgresql.conf 
#修改端口项
port=5433
[postgres@7centos7 ~]$ pg_ctl restart -D /pgsql/data2
...
server started
[postgres@7centos7 ~]$ ss -ntl | grep 543
LISTEN     0      128    127.0.0.1:5433                     *:*                  
LISTEN     0      128      [::1]:5433                  [::]:*                  


(2)
[root@7centos7 data]# service postgresqld stop
[postgres@7centos7 ~]$  /apps/pgsql/bin/pg_ctl -D /pgsql/data2 -l logfile start
[postgres@7centos7 ~]$ ss -ntl | grep 5432
LISTEN     0      128    127.0.0.1:5432                     *:*                  
LISTEN     0      128      [::1]:5432                  [::]:*                  
[postgres@7centos7 ~]$ 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值