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.
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.
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":
tail -f /var/log/postgresql/postgresql.logNote the exact file path and name mentioned in the error.
Check if the problematic file exists in the location mentioned in the error:
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.
Before deleting any files, create a backup in case you need to recover it:
cp /path/to/duplicate/file /path/to/duplicate/file.backupThis ensures you can restore it if the removal causes issues.
Delete the conflicting file from the target location:
rm /path/to/duplicate/fileIf the file is in a tablespace directory that should be empty, ensure no other files are present:
ls -la /path/to/tablespace/directory/
rm -f /path/to/tablespace/directory/* # Only if you're certain the directory should be emptyAfter removing the duplicate file, restart the PostgreSQL service to allow it to complete the pending operation:
sudo systemctl restart postgresqlOr if using a different init system:
sudo service postgresql restartCheck the PostgreSQL logs to confirm the error is resolved and the operation completed successfully:
tail -f /var/log/postgresql/postgresql.logConnect to the database and verify normal operation:
psql -U postgres -d postgres -c "SELECT version();"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.
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL
ERROR 42501: must be owner of table
How to fix "must be owner of table" in PostgreSQL
trigger cannot change partition destination
How to fix "Trigger cannot change partition destination" in PostgreSQL
SSL error: certificate does not match host name
SSL error: certificate does not match host name in PostgreSQL
No SSL connection
No SSL connection to PostgreSQL