源码编译安装postgresql

一、获取源代码

一般可以通过下载源码压缩包或者从GitHub上clone的方式获取源码

  1. 方式一:下载源码包:https://www.postgresql.org/download/.
    下载后解压即可
tar -zxvf postgresql-14.2.tar.gz
  1. 方式二:从GitHub上clone
git clone https://github.com/postgres/postgres.git

解压或下载后计入postgres目录

cd postgres

二、编译&安装

  • 目录结构
[root@10 postgres]# git remote -v
origin  https://github.com/postgres/postgres.git (fetch)
origin  https://github.com/postgres/postgres.git (push)
[root@10 postgres]# ls
aclocal.m4  config.log     configure     contrib    doc          GNUmakefile.in  Makefile  README.git
config      config.status  configure.ac  COPYRIGHT  GNUmakefile  HISTORY         README    src
[root@10 postgres]#
  • 编译,安装
./configure
make
make install

注意:configure的时候有很多编译选项,根据需要查阅文档,这里不再介绍

三、创建用户

为postgres创建用户

[root@10 postgres]# useradd postgres
[root@10 postgres]# passwd postgres
Changing password for user postgres.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

四、环境设置

切换至postgres用户

su - postgres

修改~/.bash_profile,添加三个个环境变量

export PATH=/usr/local/pgsql/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH

使环境变量生效

source ~/.bash_profile

五、创建数据库

[postgres@10 ~]$ initdb -D $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 "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /home/postgres/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 ... Asia/Shanghai
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/postgres/data -l logfile start

另外,也可以通过pg_ctl创建数据库

pg_ctl -D $PGDATA initdb

六、启动

[postgres@10 ~]$ postgres -D $PGDATA
2022-02-17 15:32:17.969 CST [77916] LOG:  starting PostgreSQL 15devel on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.3.0, 64-bit
2022-02-17 15:32:17.970 CST [77916] LOG:  listening on IPv6 address "::1", port 5432
2022-02-17 15:32:17.970 CST [77916] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2022-02-17 15:32:17.976 CST [77916] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2022-02-17 15:32:17.981 CST [77919] LOG:  database system was shut down at 2022-02-17 15:24:12 CST
2022-02-17 15:32:17.984 CST [77917] LOG:  checkpoint starting: end-of-recovery immediate wait
2022-02-17 15:32:17.998 CST [77917] LOG:  checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.001 s, sync=0.004 s, total=0.017 s; sync files=2, longest=0.003 s, average=0.002 s; distance=0 kB, estimate=0 kB
2022-02-17 15:32:18.000 CST [77916] LOG:  database system is ready to accept connections

这种方式启动的弊端是,如果终端退出或ctrl+c的话服务进程将会退出,很显然这不是我们希望看到的。因此通常使用pg_ctl方式来启动后台进程。

[postgres@10 ~]$ pg_ctl start -l logfile
waiting for server to start.... done
server started
[postgres@10 ~]$ ll
total 8
drwx------ 19 postgres postgres 4096 Feb 17 15:33 data
-rw-------  1 postgres postgres  928 Feb 17 15:33 logfile
[postgres@10 ~]$ cat logfile
2022-02-17 15:33:31.627 CST [78384] LOG:  starting PostgreSQL 15devel on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.3.0, 64-bit
2022-02-17 15:33:31.627 CST [78384] LOG:  listening on IPv6 address "::1", port 5432
2022-02-17 15:33:31.627 CST [78384] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2022-02-17 15:33:31.634 CST [78384] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2022-02-17 15:33:31.643 CST [78387] LOG:  database system was shut down at 2022-02-17 15:33:26 CST
2022-02-17 15:33:31.647 CST [78385] LOG:  checkpoint starting: end-of-recovery immediate wait
2022-02-17 15:33:31.665 CST [78385] LOG:  checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.001 s, sync=0.005 s, total=0.022 s; sync files=2, longest=0.003 s, average=0.003 s; distance=0 kB, estimate=0 kB
2022-02-17 15:33:31.669 CST [78384] LOG:  database system is ready to accept connections

屏幕输出日志保存在logfile中。

七、链接postgres

上面的动作基本上完成源码安装的过程,下面我们验证一下,通过psql链接我们启动的postgres服务。

[postgres@10 ~]$ psql -d postgres -p 5432
psql (15devel)
Type "help" for help.

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏 克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值