如何在Ubuntu上运行和切换不同版本的PostgreSQL?

huangapple go评论52阅读模式
英文:

How do I run and switch between different versions of postgreSQL on Ubuntu?

问题

我正在处理两个不同的项目,它们需要两个不同版本的PostgreSQL(12和14),这两个版本都是在安装过程中从源代码构建的。我该如何配置我的系统,以便在同一台机器上安装这两个版本,以及如何在它们之间切换?

英文:

I am working on two different projects that require two different versions of postgres (12 and 14), both of which are built from source during installation.

How can I configure my system to have both versions installed on the same machine, and how do I switch between them?

答案1

得分: 4

以下步骤可以让您在不同的PostgreSQL版本之间切换:

  1. 为数据库集群创建新目录

    sudo mkdir /usr/local/pgsql12/data
    sudo mkdir /usr/local/pgsql14/data

  2. 初始化数据库集群

     /usr/local/pgsql12/bin/initdb -D /usr/local/pgsql12/data
    
    
     /usr/local/pgsql14/bin/initdb -D /usr/local/pgsql14/data
    
  3. 启动PostgreSQL服务器

     /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
    
  4. 在不同的PostgreSQL版本之间切换

要在不同的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
英文:

Following steps can allow you to switch between different postgres versions

  1. Create New Directories for the Database Clusters
sudo mkdir /usr/local/pgsql12/data
sudo mkdir /usr/local/pgsql14/data
  1. Initialize the Database Clusters

    /usr/local/pgsql12/bin/initdb -D /usr/local/pgsql12/data
    
    
    /usr/local/pgsql14/bin/initdb -D /usr/local/pgsql14/data
    
  2. 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
    
  3. 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.

    /usr/local/pgsql12/bin/pg_ctl -D /usr/local/pgsql12/data stop


    /usr/local/pgsql14/bin/pg_ctl -D /usr/local/pgsql14/data stop

答案2

得分: 3

以下是您要翻译的内容:

当配置和初始化文件时,请确保每次将位置设置为不同的目录。假设我想安装 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

对于 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

下一步是初始化数据库并更改其中一个数据库的端口号(只有在希望同时运行两个服务器时才需要更改)。

cd /usr/local/pgsql-13
bin/initdb {your database name}
vim {your database name}/postgresql.conf

运行 vim 后,导航到大约第 64 行,您可以看到设置了端口 #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}

同样适用于其他版本(如果您没有手动更改端口号,默认情况下端口号将为 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}

如果您不同时运行两个服务器,则不必更改任何版本的端口号,但在运行另一个服务器之前,请确保停止其他服务器,使用 bin/pg_ctl -D {your database name} -l logfile 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.

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.

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:

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):

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.

答案3

得分: 2

你可以使用PostgreSQL版本管理器pgenv

卸载您当前的PostgreSQL安装,然后按照自述文件中的说明安装pgenv。

安装完成后:
使用命令pgenv available获取所有可用的PostgreSQL版本以进行安装。
然后使用pgenv build <version>来安装多个版本的PostgreSQL。

您可以随后使用pgenv use <version>pgenv switch <version>命令来使用和切换多个PostgreSQL版本。

英文:

You can you PostgreSQL Version Manager pgenv

Uninstall your current postgreSQL installation, then install pgenv following the readme instructions

After installation :
Use command pgenv available to get all the available postgres version to install.
the use pgenv build &lt;version&gt; to install multiple version of postgreSQL.

You can then use pgenv use &lt;version&gt; or pgenv switch &lt;version&gt; command to use and switch between multiple postgreSQL versions.

答案4

得分: 2

以下是已翻译的内容:

最简单的方法是通过PGENV来实现。按照README.md文件中的几个步骤,您可以轻松运行它。

使用pgenv列出可用的PostgreSQL版本。

使用pgenv install <version>安装所需的PostgreSQL版本。例如,pgenv install 12安装了PostgreSQL版本12。

使用pgenv global <version>pgenv local <version>在不同的PostgreSQL版本之间切换。pgenv global设置整个系统的默认版本,而pgenv local设置当前目录的版本。

使用以下命令验证PostgreSQL版本:

postgres --version.
英文:

The most easy way to do this is by PGENV. Following few steps on the README.md file you can run it easily.

List available PostgreSQL versions using pgenv available.

Install desired PostgreSQL versions with pgenv install &lt;version&gt;. For example, pgenv install 12 installs PostgreSQL version 12.

Switch between PostgreSQL versions using pgenv global &lt;version&gt; or pgenv local &lt;version&gt;. pgenv global sets the default version for the entire system, while pgenv local sets the version for the current directory.

Verify the PostgreSQL version with

postgres --version.

答案5

得分: 1

可以为每个数据库版本使用不同的端口。

postgresql.conf文件中,可以通过编辑port字段来编辑Postgres的端口设置。

或者在启动数据库服务器时,可以使用以下命令指定端口:

pg_ctl -D /path/to/postgres/data -l logfile -p <your_port_number> start

这个命令将在您指定的端口上启动数据库服务器。

英文:

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.

Alternatively while starting the database server you can specify the port using this command :

pg_ctl -D /path/to/postgres/data -l logfile -p &lt;your_port_number&gt; start

This command will start the database server on the port you specified.

答案6

得分: 1

首先,将不同版本保存在完全独立的目录中。

如果您想要分别运行它们,

在终端中切换到各个版本的目录,然后运行以下命令:

-> bin/pg_ctl -D -l logfile start

要停止它,请运行:

-> bin/pg_ctl -D -l logfile stop

现在,如果您想要同时运行两个版本,请更改服务器运行的端口。

为此,请打开包含您的的目录中的postgresql.conf文件,并将端口字段更改为您想要的任何值,只需确保两个版本中的端口不同。

然后,您可以从各自的目录中使用上述命令启动服务器。

英文:

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 <dbname> -l logfile start

To stop it, run,

-> bin/pg_ctl -D <dbname> -l logfile stop

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 <dbname> and change to port field to whatever you want, just make sure that both ports are different in the two versions.

And you can start the servers using the above command from inside the respective directories for both.

答案7

得分: 0

要切换版本,假设您要在PostgreSQL 12和14之间切换,您需要更改环境变量。为此,需要修改PATH和PGDATA,如下所示:
切换到PostgreSQL 12:

export PATH=/usr/local/pgsql12/bin:$PATH
export PGDATA=/usr/local/pgsql12/data

切换到PostgreSQL 14:

export PATH=/usr/local/pgsql14/bin:$PATH
export PGDATA=/usr/local/pgsql14/data

切换后还需要重新启动它。

英文:

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:

  export PATH=/usr/local/pgsql12/bin:$PATH
export PGDATA=/usr/local/pgsql12/data

For PostegreSql 14:

    export PATH=/usr/local/pgsql14/bin:$PATH
export PGDATA=/usr/local/pgsql14/data

Also restart it after switching.

答案8

得分: 0

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)
(适用于pg14)

./configure

Now install it:
现在安装它:

sudo make install -j4
sudo mkdir /usr/local/psql14/data

Now to run pg13:
现在运行pg13:

/usr/local/psql13/bin/initdb -D /usr/local/psql13/data
/usr/local/psql13/bin/pg_ctl -D /usr/local/psql13/data -l start

To run another pg, first stop the previous server by:
要运行另一个pg,首先停止先前的服务器:

/usr/local/psql13/bin/pg_ctl -D /usr/local/psql13/data -l stop

Then start the new one:
然后启动新的服务器:

/usr/local/pgsql14/bin/initdb -D /usr/local/pgsql14/data
/usr/local/psql14/bin/pg_ctl -D /usr/local/psql14/data -l start
英文:

Clone the Postgres repo

git clone https://github.com/postgres/postgres.git

checkout to the branch and select the pg version you want to install

 git branch -a

git checkout REL_13_STABLE 

(for 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:

git branch -a


 git checkout REL_14_STABLE 

(for pg14)

./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

to run another pg first stop the previous server by
/usr/local/psql13/bin/pg_ctl -D /usr/local/psql13/data -l stop

/usr/local/pgsql14/bin/initdb -D /usr/local/pgsql14/data
/usr/local/psql14/bin/pg_ctl -D /usr/local/psql14/data -l start

huangapple
  • 本文由 发表于 2023年5月14日 22:03:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定