How to Solve Error Message The data directory was initialized by PostgreSQL version 15, which is not compatible with this version 14.2 when running PostgreSQL Database Server

Posted on

Introduction

This is an article where the main content will be how to solve an error message. That error message is as appear in the title of this article. As soon as the command execution for starting the PostgreSQL database server, the error message appear. So, the following is actually a command for running the PostgreSQL database server using another PGDATA folder. That PGDATA folder is not its default PGDATA folder. It is a PGDATA folder from another PostgreSQL database server. That PGDATA folder is a PGDATA folder from an existing PostgreSQL database server after running Docker container with a PostgreSQL database docker image. That process for running the Docker container with a PostgreSQL database docker image exist in ‘How to Run PostgreSQL Database using a Docker Container and Mount the PGDATA to a Folder in a Host‘. So, the PGDATA folder was mounted to the host or the device running the docker container. In that case, the host can use that PGDATA folder. Below is the actual command execution process :

C:\Program Files\PostgreSQL\14\bin>pg_ctl.exe -D "C:\database\postgresql\pgdata" start
waiting for server to start....2023-01-21 07:36:03.185 UTC [31712] FATAL:  database files are incompatible with server
2023-01-21 07:36:03.185 UTC [31712] DETAIL:  The data directory was initialized by PostgreSQL version 15, which is not compatible with this version 14.2.
 stopped waiting
pg_ctl: could not start server
Examine the log output.

C:\Program Files\PostgreSQL\14\bin>

How to Solve Error Message The data directory was initialized by PostgreSQL version 15, which is not compatible with this version 14.2 when running PostgreSQL Database Server

Apparently, running a PostgreSQL database server with another PGDATA folder is actually possible. But it is not possible with different version of PostgreSQL database server. So, the solution is actually simple. Just use the same version of PostgreSQL database server for generating PGDDATA folder. Below are the steps for solving it in this context :

  1. Just generate another docker container but this time define the version. Just pull the different version of the PostgreSQL database server and not the latest one as follows :

    C:\Users\Personal>docker pull postgres:14.2
    14.2: Pulling from library/postgres
    Digest: sha256:2c954f8c5d03da58f8b82645b783b56c1135df17e650b186b296fa1bb71f9cfd
    Status: Image is up to date for postgres:14.2
    docker.io/library/postgres:14.2
    
    C:\Users\Personal>
    
  2. Next, just run a docker container using the docker image of PostgreSQL database server 14.2 by running the following command :

    C:\database\postgresql>docker run --name pgdb14.2 -e POSTGRES_PASSWORD=password -e PGDATA=/var/lib/postgresql/data/pgdata -v C:\database\postgresql\pgdata:/var/lib/postgresql/data/pgdata -d -p 5432:5432 postgres:14.2
    36639c3f7d8a940f9368986f18cddb027aa34375220c30d647b7543f91ebb004
    
    C:\database\postgresql>
  3. Check if the docker container is actually running by typing the following command :

    C:\Users\Personal>docker container ls -a
    CONTAINER ID IMAGE         COMMAND                CREATED    STATUS         PORTS                  NAMES
    8394377a23ab postgres:14.2 "docker-entrypoint.s…" 8 days ago Up 5 minutes   0.0.0.0:5432->5432/tcp pgdb14.2
    C:\Users\Personal>
    
  4. After that, stop that docker container by executing the following command :

    C:\Users\Personal>docker stop pgdb14.2
    pgdb14.2
    C:\Users\Personal>
    
  5. After that, check the folder which exist in ‘C:\database\postgresql\pgdata’. Fortunately, all of the PGDATA of PostgreSQL database server exist as follows :

    C:\database\postgresql\pgdata>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\database\postgresql\pgdata
    
    01/22/2023 02:02 PM <DIR> .
    01/21/2023 03:12 PM <DIR> ..
    01/21/2023 03:12 PM <DIR> base
    01/22/2023 09:58 AM <DIR> global
    01/21/2023 03:12 PM <DIR> pg_commit_ts
    01/21/2023 03:12 PM <DIR> pg_dynshmem
    01/21/2023 03:13 PM 4,821 pg_hba.conf
    01/21/2023 03:12 PM 1,636 pg_ident.conf
    01/22/2023 02:02 PM <DIR> pg_logical
    01/21/2023 03:12 PM <DIR> pg_multixact
    01/21/2023 03:12 PM <DIR> pg_notify
    01/21/2023 03:12 PM <DIR> pg_replslot
    01/21/2023 03:12 PM <DIR> pg_serial
    01/21/2023 03:12 PM <DIR> pg_snapshots
    01/22/2023 02:02 PM <DIR> pg_stat
    01/22/2023 02:02 PM <DIR> pg_stat_tmp
    01/21/2023 03:12 PM <DIR> pg_subtrans
    01/21/2023 03:12 PM <DIR> pg_tblspc
    01/21/2023 03:12 PM <DIR> pg_twophase
    01/21/2023 03:12 PM 3 PG_VERSION
    01/21/2023 03:12 PM <DIR> pg_wal
    01/21/2023 03:12 PM <DIR> pg_xact
    01/21/2023 03:12 PM 88 postgresql.auto.conf
    01/22/2023 09:55 AM 28,911 postgresql.conf
    01/22/2023 09:57 AM 111 postmaster.opts
    01/22/2023 09:56 AM <DIR> temp
    6 File(s) 35,570 bytes
    20 Dir(s) 23,822,544,896 bytes free
    
    C:\database\postgresql\pgdata>
    
  6. Finally, execute the command once more. If there are no more error messages, it will run exactly as in ‘How to Connect to PostgreSQL Database Server from Microsoft Windows using a PostgreSQL PGDATA from Linux Docker Container‘.

Leave a Reply