2

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?

tokamak32
  • 27
  • 7
  • 1
    Use the community repo [Ubuntu PG](https://www.postgresql.org/download/linux/ubuntu/) and install the packages from there. Then use [postgresql-common](https://salsa.debian.org/postgresql/postgresql-common) to manage. More info here[Debian/Ubuntu PG](https://wiki.debian.org/PostgreSql). This is a lot easier the making your own system. – Adrian Klaver May 14 '23 at 15:25
  • You can create Docker images for the different versions. This way, switching is as easy as starting or stopping a container. If you really want to build from source (why?) you can do so in an image's Dockerfile, copying only the output to the final image. This is called a [multi-stage build](https://docs.docker.com/build/building/multi-stage/) – Panagiotis Kanavos May 15 '23 at 14:02
  • Why not use one of the [official Docker images](https://hub.docker.com/_/postgres) though? There are images available for multiple distributions and architectures – Panagiotis Kanavos May 15 '23 at 14:06
  • I need access to the Postgres source code because my project involves modifying it for testing purposes. – tokamak32 May 15 '23 at 16:08

8 Answers8

4

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
Humza Tareen
  • 146
  • 6
3

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.

Ken W.
  • 397
  • 1
  • 13
2

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 <version> to install multiple version of postgreSQL.

You can then use pgenv use <version> or pgenv switch <version> command to use and switch between multiple postgreSQL versions.

Sarthak
  • 380
  • 7
2

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 <version>. For example, pgenv install 12 installs PostgreSQL version 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.

Verify the PostgreSQL version with

postgres --version.
Marcos Silva
  • 115
  • 5
1

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 <your_port_number> start

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

Omar Saad
  • 349
  • 3
  • 8
1

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

To stop it, run,

-> bin/pg_ctl -D -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 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.

0

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.

0

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