说明:
pgenv只能做到单机单实例PG。如果你要搞PG流复制等环境,需要多台机器上部署pgenv,然后各自启动一个PG实例。
它做不到单机启动多个PG实例(从这点看的话 不如自行编译的PG 可以改改路径和端口随便启动多个实例出来)
适用于临时起个测试实例。
仓库地址:https://github.com/theory/pgenv
部署很简单:
su - postgres (不支持用root启动)
git clone https://github.com/theory/pgenv.git ~/.pgenv
echo 'export PATH="$HOME/.pgenv/bin:$HOME/.pgenv/pgsql/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
常用命令:
$ pgenv
Usage: pgenv <command> [<args>]
The pgenv commands are:
use Set and start the current PostgreSQL version
start Start the current PostgreSQL server
stop Stop the current PostgreSQL server
restart Restart the current PostgreSQL server
switch Set the current PostgreSQL version
clear Stop and unset the current PostgreSQL version
build Build a specific version of PostgreSQL
rebuild Re-build a specific version of PostgreSQL
remove Remove a specific version of PostgreSQL
version Show the current PostgreSQL version
current Same as 'version'
versions List all PostgreSQL versions available to pgenv
help Show this usage statement and command summary
available Show which versions can be downloaded
check Check all program dependencies
config View, edit, delete the program configuration
log Inspects the log of the cluster, if exist.
For full documentation, see: https://github.com/theory/pgenv#readme
This is 'pgenv' version 1.3.1
$ pgenv available 列出远程仓库可用的pg 源码包的版本
# 下载不同版本的pg,并进行编译安装
$ pgenv build 13.8
$ pgenv build 15beta4
# 编译完成后,会自动部署在下面的目录下
$ pwd
/home/postgres/.pgenv
$ pgenv current 显示当前所在的版本
15beta4
$ pgenv switch 15beta4 切换到摸个版本(该操作会先停止之前的版本的运行)
$ pgenv use 列出当前可用的本地版本
Which version of PostgreSQL do you want?
13.8 pgsql-13.8
* 15beta4 pgsql-15beta4
$ pgenv use 15beta4 切到15beta4,并启动进程
Already using PostgreSQL 15beta4
PostgreSQL 15beta4 started
Logging to /home/postgres/.pgenv/pgsql/data/server.log
然后可以自行去修改pg的配置文件,例如改下shared_preload_libraries
shared_preload_libraries = 'pg_stat_statements'
$ pgenv restart 重启下pg进程
PostgreSQL 15beta4 restarted
Logging to /home/postgres/.pgenv/pgsql/data/server.log
$ pgenv log -n 20 查看最近20行的pg日志
PostgreSQL in use: 15beta4
Dumping the content of /home/postgres/.pgenv/pgsql/data/server.log
$ ps auxf 看到的进程如下
postgres 60056 0.0 0.0 335968 22564 ? Ss 10:50 0:00 /home/postgres/.pgenv/pgsql-15beta4/bin/postgres -D /home/postgres/.pgenv/pgsql/data
postgres 60057 0.0 0.0 336100 3320 ? Ss 10:50 0:00 \_ postgres: checkpointer
postgres 60058 0.0 0.0 336132 3320 ? Ss 10:50 0:00 \_ postgres: background writer
postgres 60060 0.0 0.0 336132 8520 ? Ss 10:50 0:00 \_ postgres: walwriter
postgres 60061 0.0 0.0 337576 5796 ? Ss 10:50 0:00 \_ postgres: autovacuum launcher
postgres 60064 0.0 0.0 337596 5840 ? Ss 10:50 0:00 \_ postgres: logical replication launcher
pgenv还有些其它的命令,更复杂的操作,具体可以参考官方文档。