PostgreSQL14.5源码安装

本文详细指导了如何从源代码安装PostgreSQL14.5,包括下载、依赖安装、目录创建、编译配置、用户和数据库初始化、启动与管理,以及设置开机启动。
摘要由CSDN通过智能技术生成

1.下载源码包

yum -y install wget bzip2 
wget https://ftp.postgresql.org/pub/source/v14.5/postgresql-14.5.tar.bz2 
tar xvf postgresql-14.5.tar.bz2

2.安装依赖

yum -y install zlib-devel readline-devel libxml2-devel libxslt-devel openssl-devel perl-devel perl-ExtUtils-Embed python-devel gcc-c++
依赖说明:
  zlib-devel :备份时使用的压缩功能
 readline-devel :在 psql 中使用上下方向键把历史命令找出来
 libxml2-devel :使用 xml 数据类型
  libxslt-devel:使用 libxslt
  openssl-devel:支持使用 SSL 连接加密
  perl-devel:使用 Perl 语言来开发
  python-devel :使用 Python 语言来开发

3.创建安装目录

mkdir -p /usr/local/pgsql14.5

4.编译postgresql源码

cd /soft/postgresql/postgresql-14.5

./configure --prefix=/usr/local/pgsql14.5 --with-perl --with-python --with-libxml --with-libxslt --with-openssl #可视情况加入后面的参数[--with-blocksize=32] [--with-wal-blocksize=32][--with-wal-segsize=64]
--prefix :指定安装目录
--with-perl: 使用 Perl 语言来编写自定义函数,使用该项要先安装 perl 开发包
(perl-devel)
  --with-python:使用 Python 语言来编写自定义函数,使用该选项要先安装
python-dev 开发包(python-devel)
  --with-libxml :使用 xml 数据类型,使用该选项要先安装 python-dev 开发包
(libxml2-devel)
  --with-libxslt :使用 libxslt 构建,启用 xml2 模块从而可以从 xml 到 xsl 的转换。
 PostgreSQL 在数据仓库使用场景中, 较大的数据块以提高 I/O 性能。
  --with-blocksize:指定数据块为 32KB,默认是 8KB
  --with-wal-blocksize:指定 WAL 日志块为 32KB,默认为 8KB
  --with-wal-segsize: 指定 WAL 日志文件为 64MB,默认是 16MB

make && make install

5.创建软连接

注:方便升级使用

cd /usr/local 
ln -s /usr/local/pgsql14.5 /usr/local/pgsql

6.初始化pg数据库

1)添加用户和组

groupadd -g 1001 pg #添加一个组

useradd -g 1001 –u 1001 -m pg #添加一个用户
passwd pg设置密码

2)添加环境变量

pgdata:数据目录
pghost:数据库主机或 socket 目录(默认:"本地接口")
pgport:数据库服务器的端口(默认:"5432")
pgdatabase: 指定要连接的数据库 (默认:“postgres")
pguser:指定数据库用户名(默认:“postgres")
su - pg
mkdir -p   /home/postgres/pgdata/
vi .bashrc 对 当前用户生效
export PATH=/usr/local/pgsql/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/pgsql/lib
export PGDATA=/home/postgres/pgdata/
source .bashrc
su - root
vi /etc/profile 对 所有用户生效
export PATH=/usr/local/pgsql/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/pgsql/lib
source /etc/profile

3)初始化pg

initdb -D datadir -E encoding --locale LOCALE -W [-k]
-D 指定数据目录,必选项
-E 设置数据库的默认编码,实际是设置 template1 的编码
--locale 设置区域,即设置默认语言环境, locale 就是某一个地域内的人们的语言习惯和
文化传统和生活习惯。
-W 在 initdb 过程中,为超级用户设置一个密码

-k 使用数据页产生效验和

initdb -D  /home/postgres/pgdata -E UTF8 --locale "C" -W

[pg@localhost home]$ initdb -D  /home/postgres/pgdata -E UTF8 --locale "C" -W
The files belonging to this database system will be owned by user "pg".
This user must also own the server process.

......
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/postgres/pgdata -l logfile start

#启动pg服务
[pg@localhost pgdata]$ pg_ctl -D /home/postgres/pgdata -l logfile start
waiting for server to start.... done
server started
[pg@localhost pgdata]$ ss -ltn |grep 5432
LISTEN     0      128    127.0.0.1:5432                     *:*
LISTEN     0      128        ::1:5432                    :::*
[pg@localhost pgdata]$

 4)开启checksum功能

checksum 是针对数据可靠性很高的场景(金融),开启后数据库会产生更多的 WAL 日志
pg_checksums 查看是否打开 checksum 功能
pg_checksums -e -P 启用 checksums 功能
[postgres@localhost pgdata]$  pg_ctl -D /home/postgres/pgdata/ -l logfile stop
waiting for server to shut down.... done
server stopped
[postgres@localhost pgdata]$ pg_checksums -e -P
25/25 MB (100%) computed
Checksum operation completed
Files scanned:  931
Blocks scanned: 3210
pg_checksums: syncing data directory
pg_checksums: updating control file
Checksums enabled in cluster
[postgres@localhost pgdata]$ pg_checksums
Checksum operation completed
Files scanned:  931
Blocks scanned: 3210
Bad checksums:  0
Data checksum version: 1
[postgres@localhost pgdata]$  pg_ctl -D /home/postgres/pgdata/ -l logfile start
waiting for server to start.... done
server started

5)进入pg数据库

[postgres@localhost pgdata]$ psql
psql (14.5)
Type "help" for help.

postgres=# 
postgres=# show data_directory; #查看数据目录
    data_directory
-----------------------
 /home/postgres/pgdata
(1 row)


postgres=# \l #查看数据库信息
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     |
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
(3 rows)

6)编译插件

cd postgresql-14.5/contrib 
make 
make install

[postgres@localhost extension]$ psql
psql (14.5)
Type "help" for help.

postgres=# select * from pg_available_extensions; #查看插件
        name        | default_version | installed_version |                                comment
--------------------+-----------------+-------------------+------------------------------------------------------------------------
......
ltree_plpython2u   | 1.0             |                   | transform between ltree and plpython2u
 ltree_plpython3u   | 1.0             |                   | transform between ltree and plpython3u
(64 rows)

7.启停pg数据库

管理工具:pg_ctl

1)启动pg数据库
pg_ctl start -D 数据目录 
postgres -D 数据目录 or postmaster -D 数据目录 
pg_ctl restart 重启数据库

2)停止pg数据库
pg_ctl stop -D 数据目录 [-m smart|fast(默认)|immediate] 
 smart:等所有连接中止后, 关闭数据库。 如果客户端连接不终止, 则无法关闭数
据库。 
fast:快速关闭数据库, 断开客户端的连接, 让已有的事务回滚, 然后正常关闭数
据库。 
immediate:立即关闭数据库, 相当于数据库进程立即停止, 直接退出, 下次启动
数据库需要进行恢复。 
实际上在关闭数据库的时候是直接向数据库的主进程发送 signal 信号,有以下 3 种类型: 
SIGTERM: 发送此信号为 Smart Shutdown 关机模式。 
SIGINT: 发送此信号为 Fast Shutdown 关机模式。 
SIGQUIT: 发送此信号为 Immediate Shutdown 关机模式。 

pg_ctl kill TERM|INT|QUIT pid 
kill -sigterm pid

3)查看pg数据库状态
pg_ctl status -D datadir 
psql -h -p -d –U 
pg_isready

8.设置开机启动

su - root 
cd /soft/postgresql-14.5/contrib/start-scripts  #源码包解压目录
cp linux /etc/init.d/postgres 
chmod +x /etc/init.d/postgres 

vi /etc/init.d/postgres 
PGDATA="/home/postgres/pgdata" 

service postgres start|stop|restart 

chkconfig --list 
chkconfig postgres on/off

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值