一般可以通过下载源码压缩包或者从GitHub上clone的方式获取源码
tar -zxvf postgresql-14.2.tar.gz
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中。
上面的动作基本上完成源码安装的过程,下面我们验证一下,通过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)