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?
5
Answers
You can use different port for each Database version.
Postgres port can be edited in the
postgresql.conf
file by editingport
field in that file.Alternatively while starting the database server you can specify the port using this command :
This command will start the database server on the port you specified.
Following steps can allow you to switch between different postgres versions
Initialize the Database Clusters
Start the PostgreSQL Servers
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.
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
.For pgsql 15 I would like to install it in a different directory called
pgsql-15
.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).
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:Likewise for the other version (port number will be 5432 by default if you did not manually change it):
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
.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>
orpgenv switch <version>
command to use and switch between multiple postgreSQL versions.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.