这里为大家介绍一个工具 pgenv,可以在一台机器上面创建多个 PostgreSQL 实例,并且可以自由切换。类似 NodeJS 中的 nvm 及 Python 中的 pyenv,非常的方便,推荐大家使用。
git clone https://github.com/theory/pgenv.git ~/.pgenv
echo 'export PATH="$HOME/.pgenv/bin:$HOME/.pgenv/pgsql/bin:$PATH"' >> ~/.bash_profile
# 检查系统是否缺少相应的工具链
[lavenliu@developing ~]$ pgenv check
[OK] make: /usr/bin/make
[OK] curl: /usr/bin/curl
[OK] tar: /usr/bin/tar
[OK] sed: /usr/bin/sed
[OK] sort: /usr/bin/sort
[OK] tr: /usr/bin/tr
[OK] tail: /usr/bin/tail
[KO] patch: NOT found!
# 根据上面的输出可以看到,系统上面缺少了 patch 命令,接下来安装即可。
# 安装之后再次进行检查
[lavenliu@developing ~]$ pgenv check
[OK] make: /usr/bin/make
[OK] curl: /usr/bin/curl
[OK] patch: /usr/bin/patch
[OK] tar: /usr/bin/tar
[OK] sed: /usr/bin/sed
[OK] sort: /usr/bin/sort
[OK] tr: /usr/bin/tr
[OK] tail: /usr/bin/tail
查看有哪些版本可供安装:
[lavenliu@developing ~]$ pgenv available
Available PostgreSQL Versions
========================================================
PostgreSQL 7.0
------------------------------------------------
7.0 7.0.1 7.0.2 7.0.3
......
PostgreSQL 14
------------------------------------------------
14.0 14.1 14.2 14.3 14.4 14.5
14.6 14.7 14.8 14.9 14.10 14.11
PostgreSQL 15
------------------------------------------------
15.0 15.1 15.2 15.3 15.4 15.5
15.6
PostgreSQL 16
------------------------------------------------
16.0 16.1 16.2
接下来安装指定的版本:
# 安装 16.0 版本
pgenv build 16.2
# 安装 15.1 版本
pgenv build 15.1
如果安装完成,会有如下输出:
PostgreSQL 16.2 built
安装完毕,可以使用 use 命令来使用某一个版本:
[lavenliu@developing ~]$ pgenv use 16.2
The files belonging to this database system will be owned by user "lavenliu".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
creating directory /home/lavenliu/.pgenv/pgsql/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:
/home/lavenliu/.pgenv/pgsql/bin/pg_ctl -D /home/lavenliu/.pgenv/pgsql/data -l logfile start
PostgreSQL 16.2 started
Logging to /home/lavenliu/.pgenv/pgsql/data/server.log
从上面的输出可以看到,use 命令会初始化数据库并且启动它。我们看下系统中的进程:
[lavenliu@developing ~]$ ps -ef |grep postgres
lavenliu 1032781 1 0 13:01 ? 00:00:00 /home/lavenliu/.pgenv/pgsql-16.2/bin/postgres -D /home/lavenliu/.pgenv/pgsql/data
lavenliu 1032782 1032781 0 13:01 ? 00:00:00 postgres: checkpointer
lavenliu 1032783 1032781 0 13:01 ? 00:00:00 postgres: background writer
lavenliu 1032785 1032781 0 13:01 ? 00:00:00 postgres: walwriter
lavenliu 1032786 1032781 0 13:01 ? 00:00:00 postgres: autovacuum launcher
lavenliu 1032787 1032781 0 13:01 ? 00:00:00 postgres: logical replication launcher
lavenliu 1033872 535361 0 13:02 pts/18 00:00:00 grep --color=auto postgres
如何切换版本?在切换版本之前,我们需要停止之前运行的实例:
[lavenliu@developing ~]$ pgenv stop
PostgreSQL 16.2 stopped
此时再次执行 use 命令:
[lavenliu@developing ~]$ pgenv use 15.1
# 再次查看一下进程信息
[lavenliu@developing ~]$ ps -ef |grep postgres
lavenliu 1087543 1 0 13:23 ? 00:00:00 /home/lavenliu/.pgenv/pgsql-15.1/bin/postgres -D /home/lavenliu/.pgenv/pgsql/data
lavenliu 1087544 1087543 0 13:23 ? 00:00:00 postgres: checkpointer
lavenliu 1087545 1087543 0 13:23 ? 00:00:00 postgres: background writer
lavenliu 1087547 1087543 0 13:23 ? 00:00:00 postgres: walwriter
lavenliu 1087548 1087543 0 13:23 ? 00:00:00 postgres: autovacuum launcher
lavenliu 1087549 1087543 0 13:23 ? 00:00:00 postgres: logical replication launcher
lavenliu 1088121 535361 0 13:23 pts/18 00:00:00 grep --color=auto postgres
我们可以进入到 pgenv 的家目录看下:
[lavenliulavenliu@developing ~]$ pwd
/home/lavenliu/.pgenv
[lavenliu@developing ~]$ ls -l
total 68
drwxr-xr-x 2 lavenliu lavenliu 4096 Apr 18 13:15 bin
drwxr-xr-x 2 lavenliu lavenliu 4096 Apr 18 13:46 config
-rw-r--r-- 1 lavenliu lavenliu 1081 Apr 18 09:14 LICENSE.md
drwxr-xr-x 5 lavenliu lavenliu 4096 Apr 18 09:14 patch
lrwxrwxrwx 1 lavenliu lavenliu 11 Apr 18 13:48 pgsql -> pgsql-13.10
drwxr-xr-x 7 lavenliu lavenliu 4096 Apr 18 13:48 pgsql-13.10
drwxr-xr-x 7 lavenliu lavenliu 4096 Apr 18 13:42 pgsql-14.10
drwxr-xr-x 7 lavenliu lavenliu 4096 Apr 18 13:23 pgsql-15.1
drwxr-xr-x 7 lavenliu lavenliu 4096 Apr 18 13:01 pgsql-16.2
-rw-r--r-- 1 lavenliu lavenliu 31885 Apr 18 09:14 README.md
drwxr-xr-x 6 lavenliu lavenliu 4096 Apr 18 13:43 src
在切换版本的时候,pgenv 的脚本在背后就是做了软链接的替换。
如何进入刚刚创建的数据库呢?直接在命令行执行 psql 即可。
[lavenliu@developing ~]$ psqlpsql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: role "lavenliu" does not exist
提示报错了,说是不存在 “lavenliu” 角色。因为 PostgreSQL 在安装的时候会创建一个超级用户(或角色) postgres,并不会创建其它用户,因此会报错。这时,我们可以指定用 postgres 进行登录,如下:
[lavenliu@developing ~]$ psql -U postgres
psql (16.2)
Type "help" for help.
postgres=#
现在我们已经登录到了数据库中,可以查看一下当前有哪些数据库及用户:
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | ICU Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
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/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(3 rows)
postgres=# \du
List of roles
Role name | Attributes
-----------+------------------------------------------------------------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS
还记得刚才的 role “lavenliu” does not exist 错误吗?现在我们把它创建出来。操作如下:
postgres=# create user lavenliu with password 'lavenliu';
CREATE ROLE
postgres=# create database lavenliu owner lavenliu;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | ICU Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
lavenliu | lavenliu | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
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/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(4 rows)
接下来我们使用 lavenliu 普通用户进行登录,先退出 postgres 超级用户。如下:
postgres=# \q
$ psql -U lavenliu
psql (16.2)
Type "help" for help.
lavenliu=>
好了,关于 pgenv 的使用就介绍到这里,欢迎大家继续关注后续新的内容。
564






