Error invalid tablespace location when migrating to PostgreSQL 15

Problem

When migrating to PostgreSQL 15, an error occurs: the archive database location is incorrect:

ERROR: invalid tablespace location: /var/lib/postgresql/11/archive_2023 (name=archive_2023, owner=postgres)

Cause

The symlink to the archive databases was created according to outdated instructions and is located in the paths of the old cluster /var/lib/postgresql/11/.

Find out the location of the database:

  1. Run SQL:

    ibaytr sql
    
  2. In the psql console, run the SQL query:

    SELECT spcname AS "Name", pg_catalog.pg_get_userbyid(spcowner) AS "Owner", pg_catalog.pg_tablespace_location(oid) AS "Location" FROM pg_catalog.pg_tablespace ORDER BY 1;
    

Example output:

 Name         |  Owner   |              Location
--------------+----------+-------------------------------------
 archive_2023 | postgres | /var/lib/postgresql/11/archive_2023
 pg_default   | postgres |
 pg_global    | postgres |
 (3 строки)

 ibatyrr=>

The archive_2023 database is located in the old cluster /var/lib/postgresql/11/.

Solution

Recreate the symlink to the mounted volume.

  1. Stop PostgreSQL:

    sudo systemctl stop postgresql@11-main
    
  2. Find out the name of the current symlink to the archive database, in our case archive_2023:

    sudo ls -l /var/lib/postgresql/11/main/pg_tblspc
    total 0
    lrwxrwxrwx 1 postgres postgres 35 июн  3 22:18 24939407 -> /var/lib/postgresql/11/archive_2023
    
  3. Remove the symlink:

    sudo rm /var/lib/postgresql/11/main/pg_tblspc/24939407
    
  4. Check that everything has been deleted:

    sudo ls -l /var/lib/postgresql/11/main/pg_tblspc
    
  5. Create a new symlink:

    sudo ln -sf /mnt/archive_2023/ /var/lib/postgresql/11/main/pg_tblspc/24939407
    
  6. Make sure that the symlink is created and points to the desired directory:

    sudo ls -l /var/lib/postgresql/11/main/pg_tblspc
    total 0
    lrwxrwxrwx 1 root root 18 июл  1 18:41 24939407 -> /mnt/archive_2023/
    
  7. Resume PostgreSQL:

    sudo systemctl start postgresql@11-main
    
  8. Log into the psql console again and check all tablespaces:

    ibatyr sql
    
    ibatyr sql
    psql (11.20 (Ubuntu 11.20-1.pgdg18.04+1))
    
  9. Get help:

    help
    

    Example output:

    ibatyr=> SELECT spcname AS "Name", pg_catalog.pg_get_userbyid(spcowner) AS "Owner", pg_catalog.pg_tablespace_location(oid) AS "Location" FROM pg_catalog.pg_tablespace ORDER BY 1;
         Name     |  Owner   |      Location
    --------------+----------+--------------------
     archive_2023 | postgres | /mnt/archive_2023/
     pg_default   | postgres |
     pg_global    | postgres |
    (3 строки)
    
    ibatyr=>
    

Now the database is located in the correct directory - /mnt/archive_2023/.