1 postgresql简介
postgresql的口号是:PostgreSQL: The world's most advanced open source database
官网地址是,https://www.postgresql.org/
https://www.postgresql.org/ftp/source/放置了各个版本的代码,截止2024年12月,最新版本是17.2,地址是https://ftp.postgresql.org/pub/source/v17.2/postgresql-17.2.tar.gz
2 下载代码
使用wget下载postgresql代码的tar压缩包文件,使用tar命令解压缩。
wget https://ftp.postgresql.org/pub/source/v17.2/postgresql-17.2.tar.gz
tar zxvf postgresql-17.2.tar.gz
3 编译代码
3.1 准备工具
make >=3.81
flex >=2.5.35
Bison >=2.3
perl >= 5.14
3.2 目录设计
本文在/postgresql下面,解压缩源码文件包后产生源码文件夹postgresql-17.2,还需要建立postgresql-17.2-build、postgresql-17.2-install和postgresql-17.2-data目录,分别专门用于编译目录、安装目录和数据目录。
mkdir postgresql-17.2-build
mkdir postgresql-17.2-install
mkdir postgresql-17.2-data
3.3 开始编译
cd postgresql-17.2-build
../postgresql-17.2/configure CFLAGS='-O0' --enable-debug --prefix=/postgres/postgresql-17.2-install
make -j4
make install
cd ..
这里使用了环境变量CFLAGS='-O0',以及开启--enable-debug选项,目的是在使用gcc编译程序时,加入参数-O0和-g2这2个选项,便于调试程序。在生产环境上,不要开启这2个参数。
3.4 切换到postgres用户
切换到postgres用户,且把install和data目录,均切换为postgres用户所有。
groupadd postgres
useradd postgres -g postgres -G postgres
chown -R postgres:postgres postgresql-17.2-install
chown -R postgres:postgres postgresql-17.2-data
4 初始化和启动
最简单的初始化,是使用initdb命令,加一个Data目录(需要是空文件夹)。
[postgres@centos7 postgres]$ /postgres/postgresql-17.2-install/bin/initdb -D /postgres/postgresql-17.2-data
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.
fixing permissions on existing directory /postgres/postgresql-17.2-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
initdb: hint: 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:
/postgres/postgresql-17.2-install/bin/pg_ctl -D /postgres/postgresql-17.2-data -l logfile start
[postgres@centos7 postgres]$
启动命令也很简单,初始化最后的提示,使用命令/postgres/postgresql-17.2-install/bin/pg_ctl -D /postgres/postgresql-17.2-data -l logfile start来启动。
[postgres@centos7 postgres]$ /postgres/postgresql-17.2-install/bin/pg_ctl -D /postgres/postgresql-17.2-data -l logfile start
waiting for server to start.... done
server started
[postgres@centos7 postgres]$ ps -ef | grep postgres
root 1289 1270 0 09:50 ? 00:00:01 runsv postgresql
root 1341 1289 0 09:50 ? 00:00:00 svlogd -tt /var/log/gitlab/postgresql
root 13105 11366 0 09:55 pts/1 00:00:00 su - postgres
postgres 13111 13105 0 09:55 pts/1 00:00:00 -bash
postgres 56662 1 0 10:11 ? 00:00:00 /postgres/postgresql-17.2-install/bin/postgres -D /postgres/postgresql-17.2-data
postgres 56663 56662 0 10:11 ? 00:00:00 postgres: checkpointer
postgres 56664 56662 0 10:11 ? 00:00:00 postgres: background writer
postgres 56666 56662 0 10:11 ? 00:00:00 postgres: walwriter
postgres 56667 56662 0 10:11 ? 00:00:00 postgres: autovacuum launcher
postgres 56668 56662 0 10:11 ? 00:00:00 postgres: logical replication launcher
postgres 57000 13111 0 10:11 pts/1 00:00:00 ps -ef
postgres 57001 13111 0 10:11 pts/1 00:00:00 grep --color=auto postgres
[postgres@centos7 postgres]$ date
Fri Dec 6 10:11:29 CST 2024
在这个例子中,通过/postgres/postgresql-17.2-install/bin/postgres -D /postgres/postgresql-17.2-data启动的进程号是56662,其父进程是1,其他的5个进程都是56662的子进程,包含了checkpointer进程、backgroud writer进程、walwriter进程、autocacuum laucher进程和logical replication laucher进程。
5 客户端连接服务端
使用客户端工具psql来连接postgres服务端。
[postgres@centos7 ~]$ which psql
/postgres/postgresql-17.2-install/bin/psql
[postgres@centos7 ~]$ psql
psql (17.2)
Type "help" for help.
postgres=# \d
Did not find any relations.
postgres=# \c
You are now connected to database "postgres" as user "postgres".
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privile
ges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------
------
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres
+
| | | | | | | | postgres=CTc/pos
tgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres
+
| | | | | | | | postgres=CTc/pos
tgres
(3 rows)
postgres=#
6 关闭服务端
继续使用pg_ctl来关闭服务端。
[postgres@centos7 postgres]$ /postgres/postgresql-17.2-install/bin/pg_ctl -D /postgres/postgresql-17.2-data stop
waiting for server to shut down.... done
server stopped