How to fix Duplicate file in PostgreSQL

PostgreSQLADVANCEDHIGHUpdated Dec 28, 2025ยท 109 views

The "Duplicate file" error (58P02) occurs when PostgreSQL encounters a file with the same name in a location where a new file is being created. This typically happens during tablespace creation or low-level file operations.

Quick fix

Fixes 58P02

tail -f /var/log/postgresql/postgresql.log
  1. Check the PostgreSQL logs for the specific file name
  2. Verify the file exists in the target location
  3. Back up the problematic file before removal
  4. Remove the duplicate or stale file
postgresql โ€” error
tail -f /var/log/postgresql/postgresql.log
Duplicate file

What this error means

PostgreSQL error code 58P02 (duplicate_file) is a system-level error that occurs when the database engine attempts to create or copy a file to a location where a file with the same name already exists. This is a file system conflict rather than a data constraint issue. The error indicates that PostgreSQL tried to perform a file operation but the target location was not empty or available as expected. This commonly occurs during administrative operations like tablespace creation, WAL file management, or when extensions perform low-level file system interactions.

Typical symptoms

  • Error message contains "duplicate file" or error code 58P02
  • Tablespace creation fails with file already exists error
  • Database startup fails due to conflicting files in data directory
  • Backup or restoration operations report duplicate file conflicts
  • Database logs show repeated "duplicate file" warnings during initialization

Common causes

  1. Tablespace directory is not empty when creating a new tablespace
  2. Residual files remain from a failed previous operation in the target directory
  3. Backup tools or scripts created duplicate files in the PostgreSQL data directory
  4. Manual file operations left conflicting files that PostgreSQL cannot overwrite
  5. WAL file duplication or leftover temporary files from crashed operations

How to fix "Duplicate file"

1Check the PostgreSQL logs for the specific file name

Look at your PostgreSQL log files to identify which file is causing the duplicate error. The logs will contain a message like "could not create file" or "duplicate file detected":

bash
tail -f /var/log/postgresql/postgresql.log

Note the exact file path and name mentioned in the error.

2Verify the file exists in the target location

Check if the problematic file exists in the location mentioned in the error:

bash
ls -la /path/to/postgresql/datadir/
ls -la /path/to/tablespace/directory/

Confirm that the file is indeed present and determine if it is safe to remove.

3Back up the problematic file before removal

Before deleting any files, create a backup in case you need to recover it:

bash
cp /path/to/duplicate/file /path/to/duplicate/file.backup

This ensures you can restore it if the removal causes issues.

4Remove the duplicate or stale file

Delete the conflicting file from the target location:

bash
rm /path/to/duplicate/file

If the file is in a tablespace directory that should be empty, ensure no other files are present:

bash
ls -la /path/to/tablespace/directory/
rm -f /path/to/tablespace/directory/*  # Only if you're certain the directory should be empty
5Restart PostgreSQL to complete the operation

After removing the duplicate file, restart the PostgreSQL service to allow it to complete the pending operation:

bash
sudo systemctl restart postgresql

Or if using a different init system:

bash
sudo service postgresql restart
6Verify the operation succeeded

Check the PostgreSQL logs to confirm the error is resolved and the operation completed successfully:

bash
tail -f /var/log/postgresql/postgresql.log

Connect to the database and verify normal operation:

bash
psql -U postgres -d postgres -c "SELECT version();"

Advanced notes

File system permissions are critical when removing files from PostgreSQL directories. Always ensure you have proper backups before deleting any database files. If the duplicate file is a WAL (Write-Ahead Log) file, consult your backup strategy and point-in-time recovery (PITR) needs before removal. For tablespace operations, ensure the target directory is properly owned by the postgres system user (typically uid 26 or 999 depending on your system) with permissions 0700. If you're using Barman for backups, duplicate files may appear in backup directories under an "errors" subdirectory; these can typically be safely removed. Always test critical operations in a development environment first.

Frequently asked questions

What does "58P02" mean?
PostgreSQL error code 58P02 (duplicate_file) is a system-level error that occurs when the database engine attempts to create or copy a file to a location where a file with the same name already exists. This is a file system conflict rather than a data constraint issue. The error indicates that PostgreSQL tried to perform a file operation but the target location was not empty or available as expected.
How do I fix "58P02"?
The "Duplicate file" error (58P02) occurs when PostgreSQL encounters a file with the same name in a location where a new file is being created. This typically happens during tablespace creation or low-level file operations.
What causes "58P02"?
Tablespace directory is not empty when creating a new tablespace Residual files remain from a failed previous operation in the target directory Backup tools or scripts created duplicate files in the PostgreSQL data directory
Is the "58P02" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading