Following steps can allow you to switch between different postgres versions
通过以下步骤可以在不同的Postgres版本之间进行切换
- Create New Directories for the Database Clusters
sudo mkdir /usr/local/pgsql12/data
sudo mkdir /usr/local/pgsql14/data
Initialize the Database Clusters
/usr/local/pgsql12/bin/initdb -D /usr/local/pgsql12/data
/usr/local/pgsql14/bin/initdb -D /usr/local/pgsql14/data
Start the PostgreSQL Servers
/usr/local/pgsql12/bin/pg_ctl -D /usr/local/pgsql12/data -l logfile12 start
/usr/local/pgsql14/bin/pg_ctl -D /usr/local/pgsql14/data -l logfile14 start
Switch Between PostgreSQL Versions
To switch between PostgreSQL versions, you'll need to stop the currently running server and then start the server for the other version.
要在不同的PostgreSQL版本之间切换,您需要停止当前运行的服务器,然后启动其他版本的服务器。
/usr/local/pgsql12/bin/pg_ctl -D /usr/local/pgsql12/data stop
/usr/local/pgsql14/bin/pg_ctl -D /usr/local/pgsql14/data stop
When configuring and initializing the files, ensure the location is set to a different directory each time. Let's say I would like pgsql 13 and 15 installed. For the code block below, I have configured pgsql 13 to be installed in a directory called pgsql-13
.
配置和初始化文件时,请确保每次都将位置设置为不同的目录。假设我希望安装pgsql 13和15。对于下面的代码块,我将pgsql 13配置为安装在名为pgsql-13的目录中。
git clone https://github.com/postgres/postgres.git
cd postgres
git checkout REL_13_STABLE
./configure --prefix=/usr/local/pgsql-13
make
sudo mkdir /usr/local/pgsql-13
sudo chown {your username} /usr/local/pgsql-13
make install
export PATH=/usr/local/pgsql-13/bin/:$PATH
export PGDATA=/usr/local/pgsql-13/bin/data
For pgsql 15 I would like to install it in a different directory called pgsql-15
.
对于pgsql 15,我想将其安装在一个名为pgsql-15的不同目录中。
git checkout REL_15_STABLE
./configure --prefix=/usr/local/pgsql-15
make
sudo mkdir /usr/local/pgsql-15
sudo chown {your username} /usr/local/pgsql-15
make install
export PATH=/usr/local/pgsql-15/bin/:$PATH
export PGDATA=/usr/local/pgsql-15/bin/data
The next step would be to initialize the database and change the port number for one of the databases (only if you want to be able to run both servers at the same time).
下一步是初始化数据库并更改其中一个数据库的端口号(只有在您希望能够同时运行两个服务器的情况下)。
cd /usr/local/pgsql-13
bin/initdb {your database name}
vim {your database name}/postgresql.conf
After running vim
, navigate to around line 64 where you can see the port set #port = 5432
. Delete the hashtag #
and change the port number to something else such as 5431. Save and exit the editor to start the server and create the database using:
运行vim之后,导航到第64行附近,在那里您可以看到端口set#port=5432。删除标签#并将端口号更改为其他值,如5431。保存并退出编辑器以启动服务器并使用以下命令创建数据库:
bin/pg_ctl -D {your database name} -l logfile start
bin/createdb --port=5431 {your database name}
bin/psql --port=5431 {your database name}
Likewise for the other version (port number will be 5432 by default if you did not manually change it):
对于其他版本也是如此(如果未手动更改,则默认端口号为5432):
cd /usr/local/pgsql-15
bin/initdb {your database name}
bin/pg_ctl -D {your database name} -l logfile start
bin/createdb --port=5432 {your database name}
bin/psql --port=5432 {your database name}
If you're not running both servers at the same time, you don't have to change the port numbers for either versions, but make sure the other server is stopped before running the other one using bin/pg_ctl -D {your database name} -l logfile stop
.
如果您没有同时运行两个服务器,则不必更改任何一个版本的端口号,但请确保在运行另一个服务器之前使用bin/pg_ctl-D{您的数据库名称}-L日志文件STOP停止另一个服务器。
You can you PostgreSQL Version Manager pgenv
您可以使用PostgreSQL版本管理器pgenv
Uninstall your current postgreSQL installation, then install pgenv following the readme instructions
卸载当前的PostgreSQL安装,然后按照自述文件说明安装pgenv
After installation :
Use command pgenv available
to get all the available postgres version to install.
the use pgenv build <version>
to install multiple version of postgreSQL.
安装后:使用可用命令pgenv获取要安装的所有可用的postgres版本。使用pgenv Build
安装多个版本的PostgreSQL。
You can then use pgenv use <version>
or pgenv switch <version>
command to use and switch between multiple postgreSQL versions.
然后,您可以使用pgenv use或pgenv Switch命令在多个PostgreSQL版本之间使用和切换。
The most easy way to do this is by PGENV. Following few steps on the README.md file you can run it easily.
要做到这一点,最简单的方法是PGENV。在Readme.md文件上执行几个步骤,您就可以轻松地运行它。
List available PostgreSQL versions using pgenv
available.
使用可用的pgenv列出可用的PostgreSQL版本。
Install desired PostgreSQL versions with pgenv install <version>
. For example, pgenv install 12 installs PostgreSQL version 12
.
使用pgenv Install
安装所需的PostgreSQL版本。例如,pgenv Install 12会安装PostgreSQL版本12。
Switch between PostgreSQL versions using pgenv global <version>
or pgenv local <version>
. pgenv global
sets the default version for the entire system, while pgenv local sets the version for the current directory.
使用pgenv global或pgenv local切换PostgreSQL版本。Pgenv global设置整个系统的默认版本,而pgenv local设置当前目录的版本。
Verify the PostgreSQL version with
使用验证PostgreSQL版本
postgres --version.
You can use different port for each Database version.
您可以为每个数据库版本使用不同的端口。
Postgres port can be edited in the postgresql.conf
file by editing port
field in that file.
通过编辑postgresql.conf文件中的端口字段,可以在该文件中编辑postgres端口。
Alternatively while starting the database server you can specify the port using this command :
或者,在启动数据库服务器时,您可以使用以下命令指定端口:
pg_ctl -D /path/to/postgres/data -l logfile -p <your_port_number> start
This command will start the database server on the port you specified.
此命令将在您指定的端口上启动数据库服务器。
First of all, keep different versions in completely separate directories.
首先,将不同版本保存在完全独立的目录中。
If you want to run them separately,
如果您想单独运行它们,
change to the directory of the respective version in terminal and run the following command:
切换到终端中相应版本的目录,并运行以下命令:
-> bin/pg_ctl -D -l logfile start
->bin/pg_ctl-D-L日志文件启动
To stop it, run,
要阻止它,就跑吧,
-> bin/pg_ctl -D -l logfile stop
-> bin/pg_ctl -D -l日志文件停止
Now, if you want to run both versions simultaneously, change the port on which the servers run.
现在,如果您想同时运行这两个版本,请更改运行服务器的端口。
For this open the postgresql.conf file which is inside the directory with your and change to port field to whatever you want, just make sure that both ports are different in the two versions.
为此,打开您的目录中的postgresql.conf文件,并将端口字段更改为您想要的任何内容,只需确保两个版本中的两个端口不同即可。
And you can start the servers using the above command from inside the respective directories for both.
您可以使用上面的命令从这两个服务器各自的目录中启动服务器。
In order to switch between the version, lets suppose you want to switch between Postgresql 12 and 14, you need to change the environment variables. The modification in PATH and PGDATA is needed for this purpose like:
switch between PostgreSQL 12 and PostgreSQL 14, you'll need to change the environment variables. You can do this by modifying the PATH and PGDATA variables:
For PostgreSql 12:
为了在版本之间切换,假设您想要在PostgreSQL 12和14之间切换,您需要更改环境变量。为此,需要对PATH和PGDATA进行修改,例如:在PostgreSQL12和PostgreSQL14之间切换时,需要更改环境变量。您可以通过修改PATH和PGDATA变量来完成此操作:对于PostgreSQL 12:
export PATH=/usr/local/pgsql12/bin:$PATH
export PGDATA=/usr/local/pgsql12/data
For PostegreSql 14:
对于PostegreSql 14:
export PATH=/usr/local/pgsql14/bin:$PATH
export PGDATA=/usr/local/pgsql14/data
Also restart it after switching.
也可以在切换后重新启动。
Clone the Postgres repo
克隆Postgres回购
git clone https://github.com/postgres/postgres.git
checkout to the branch and select the pg version you want to install
签出到分支并选择要安装的PG版本
git branch -a
git checkout REL_13_STABLE
(for pg13)
(适用于PG13)
./configure
now install it
现在安装它
sudo make install -j4
now create a database cluster:
现在创建一个数据库集群:
sudo mkdir /usr/local/psql13/data
now install another postgres version:
现在安装另一个Postgres版本:
git branch -a
git checkout REL_14_STABLE
(for pg14)
(for第14页)
./configure
now install it
现在安装它
sudo make install -j4
sudo mkdir /usr/local/psql14/data
now to run pg13:
/usr/local/psql13/bin/initdb -D /usr/local/psql13/data
/usr/local/psql13/bin/pg_ctl -D /usr/local/psql13/data -l start
现在运行pg13:/usr/local/psql13/bin/initdb-D/usr/local/psql13/data/usr/local/psql13/bin/pg_ctl-D/usr/local/psql13/data-L启动
to run another pg first stop the previous server by
/usr/local/psql13/bin/pg_ctl -D /usr/local/psql13/data -l stop
要运行另一个PG,首先使用/usr/local/psql13/bin/pg_ctl-D/usr/local/psql13/data-L命令停止上一台服务器
/usr/local/pgsql14/bin/initdb -D /usr/local/pgsql14/data
/usr/local/psql14/bin/pg_ctl -D /usr/local/psql14/data -l start
/usr/local/pgsql14/bin/initdb-D/usr/local/pgsql14/data/usr/local/psql14/bin/pg_ctl-D/usr/local/psql14/data-L启动
更多回答
我是一名优秀的程序员,十分优秀!