PostgreSQL Error 58P02: Duplicate File Fix
In this article
Error 58P02 in PostgreSQL means the server tried to create a file that already exists, most often during a base backup, a tablespace operation, or a WAL archive restore. The fix is to stop the operation, confirm which file is duplicated, and either remove the stray copy or rerun the step so PostgreSQL writes it cleanly. Do not delete anything until you have identified the file and have a current backup.
What You'll Need
- Access to the PostgreSQL server host as the operating system user that owns the data directory (usually
postgres). - A current physical or logical backup, and a known-good copy of the database if you are restoring.
- The exact error text from the PostgreSQL log, including the file path PostgreSQL names.
- A maintenance window: some steps require stopping and restarting the server.
- Optional: a checksum tool such as
sha256sumormd5sumto compare duplicate files.
Step-by-Step
- Read the full error in the log. Open the PostgreSQL log (the location is set by
log_directoryandlogging_collectorinpostgresql.conf; on many Linux packages it is under/var/log/postgresql/). Find the line containing58P02orduplicate file. PostgreSQL normally names the file it could not create, for example a relation file underbase/or a file insidepg_tblspc/. Write that path down exactly. - Identify what operation was running. The error usually appears during
pg_basebackup,pg_rewind, aCREATE TABLESPACEorALTER TABLE ... SET TABLESPACE, a restore from a WAL archive, or a file-system-level copy. Check the log lines immediately before the error and any running maintenance job. If a backup or restore is still running, stop it cleanly before touching files. - Confirm the duplicate really is a duplicate. Using the path from the log, list the directory and compare file sizes and timestamps. On Linux, for example,
ls -lin the containing directory. If two files look like copies of the same relation or tablespace file, compare their checksums withsha256sum. Only treat a file as safe to remove if you have a backup and you are certain it is the stray copy, not the live data file. - For a failed tablespace operation, recreate the tablespace cleanly. If the error came from
CREATE TABLESPACE, check whether the target directory already contains files from an earlier attempt. The directory must be empty and owned by thepostgresuser. Remove only the leftover files from the failed attempt, then rerun the command. If the tablespace was partially created, drop it withDROP TABLESPACEbefore retrying, and make sure no objects still reference it. - For a failed base backup, delete the partial backup and rerun. A
pg_basebackupthat was interrupted can leave a partial directory. Remove that partial output directory (never the live data directory), then runpg_basebackupagain with the same options. If the error names a file inside the backup target, confirm the target was empty before the run. - For a restore or archive recovery, check for a second copy of the WAL file. If the error appears during recovery, PostgreSQL may be trying to restore a WAL segment that already exists in
pg_wal. Compare the archived segment with the one inpg_wal; if they are identical, the restore step can be skipped. If they differ, stop and investigate before overwriting anything, because a mismatched WAL file can corrupt recovery. - Restart PostgreSQL and verify. After removing the stray file or rerunning the failed step, restart the server with
pg_ctl restartor your service manager (for examplesystemctl restart postgresql). Then run a simple query such asSELECT 1;and check the log for new errors. If the error returns, do not keep deleting files: go to the troubleshooting section below.
Common Mistakes
- Deleting the live relation file instead of the stray copy. Always confirm with the log path and a checksum before removing anything. A wrong deletion can make a table unreadable.
- Removing files while the server is running. Stop the operation or the server first; deleting files underneath a running PostgreSQL can cause crashes or corruption.
- Retrying a tablespace command without emptying the directory.
CREATE TABLESPACErequires an empty directory owned by the right user; leftover files cause the same error again. - Assuming the error is harmless. A duplicate file during recovery can indicate a mismatched WAL archive, which is a data-integrity problem, not just a nuisance.
- Skipping the backup. Any file-level fix should be done only when you have a current backup you can restore.
Tips
- Keep the PostgreSQL log at a level that records the file path in errors;
log_min_messagesandlog_error_verbosityaffect how much detail you see. - Use a dedicated, empty directory for each tablespace and document its path, so retries are predictable.
- Before any backup or restore, confirm the target directory is empty and that you are not writing into the live data directory.
- Run
pg_basebackupand restore jobs with logging enabled so a failure leaves a clear record of which file caused the problem. - Keep WAL archive and restore commands idempotent where possible, so a repeated restore does not try to overwrite an existing segment.
Troubleshooting
If the error returns after you remove the file, stop and gather evidence before trying again. First, re-read the log for the exact path and the operation that triggered it; the same error can come from different causes. Second, check that the server was fully stopped before you changed files, and that no background job such as a backup script is still running. Third, verify ownership and permissions on the directory: PostgreSQL must be able to write there as its own operating system user. Fourth, if the error happens during recovery, compare the WAL segment in the archive with the one in pg_wal and do not overwrite a mismatched file; restore from a known-good backup instead. Fifth, if a tablespace is involved, confirm no database objects still point to it before dropping or recreating it. If you cannot identify which file is the stray copy, restore the affected database or cluster from your backup rather than guessing. For production systems, involve whoever owns the backup and recovery process before making file-level changes.
Frequently Asked Questions
What does PostgreSQL error 58P02 mean?
It is the duplicate file error: PostgreSQL tried to create a file that already exists. It most often appears during backups, tablespace operations, or WAL restore.
Can I just delete the duplicate file?
Only after confirming from the log which file is the stray copy and after taking a backup. Deleting the wrong file can damage a table or break recovery.
Why does CREATE TABLESPACE fail with 58P02?
The target directory must be empty and owned by the PostgreSQL operating system user. Leftover files from an earlier attempt cause the error.
Does this error mean my data is corrupted?
Not necessarily. It can be a harmless leftover file, but during recovery it may signal a mismatched WAL archive, so check before proceeding.
Should I restart PostgreSQL after fixing it?
Yes. Restart the server, run a simple query, and check the log to confirm the error is gone.
What if the error keeps coming back?
Stop deleting files. Re-check the log path, confirm no backup job is running, verify directory ownership, and restore from a known-good backup if the cause is unclear.