There are instances where a Postgres database will go down and when attempting to start the db, it will generate the error, postgres: could not find the database system
Expected to find it in the directory.
postgres: could not find the database system
Expected to find it in the directory "/test/postgres/bd/data/base/PG_15",
but could not open file "/test/postgres/bd/data/base/PG_15/global/pg_control": Permission denied
Postgres 15.6
This usually happens because of a configuration or environment change:
Wrong Ownership: The data directory was copied or moved by a different user (like root), changing the file ownership.
Incorrect Permissions: The folder permissions are too restrictive (Postgres typically expects 0700 or 0750).
Service User Mismatch: You are trying to start the database manually as yourself, but the files belong to the postgres user.
1. Identify the Tablespace OID. Postgres maps tablespaces to folders using an OID (Object ID). This will help identify the symbolic links that need to be updated.
SELECT oid, spcname FROM pg_tablespace WHERE spcname = 'your_tablespace_name';
2. Shut Down PostgreSQL. Moving files while the database is active will cause corruption.
sudo systemctl stop postgresql
3. Copy the Data (Preserving Permissions)
Use rsync to copy the data. It will preserve the ownership (usually the postgres user) and permissions (usually 0700).
Example: Moving from /old/path to /new/path
# -a preserves permissions/ownership, -v is verbose
sudo rsync -av /old/path/tablespace_data /new/path/
4. Update the Symbolic Link
Navigate to the pg_tblspc directory inside your main PostgreSQL data folder. You will see a symbolic link named after the OID from Step 1.
cd /var/lib/postgresql/15/main/pg_tblspc/
# Remove the old link
rm <OID_NUMBER>
# Create the new link pointing to the new location
ln -s /new/path/tablespace_data <OID_NUMBER>
5. Verify Ownership and Restart
Ensure the postgres user owns the new destination. If the ownership shifted to root during the copy, Postgres will give you that "Permission denied" error again.
sudo chown -R postgres:postgres /new/path/tablespace_data
sudo systemctl start postgresql