skip to Main Content

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


  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.

    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
  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.

    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
  5. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search