PostgreSQL Error 58P03: File Name Too Long
In this article
PostgreSQL error 58P03 means a file name generated by the server exceeded the operating system's limit, so the operation was rejected. The fix is to shorten the path or name involved, not to raise a PostgreSQL setting. You will usually see it during a restore, a tablespace move, a WAL archive copy, or when a query spills to temporary files on a deeply nested directory. The error code 58P03 is defined as ERRCODE_FILE_NAME_TOO_LONG and comes from the server's own file-name length check, so it can appear even when the underlying filesystem would technically accept a longer name.
What You'll Need
- Access to the PostgreSQL server host (SSH or local shell) and permission to read the data directory and log files.
- The PostgreSQL server log entry for the failure, which names the file or directory involved.
- Knowledge of the operating system's path limit: on Linux, a single path component is typically limited to 255 bytes and the full path to 4096 bytes; on Windows, the default maximum path is 260 characters unless long paths are enabled.
- A maintenance window if the fix requires moving a tablespace, changing
temp_tablespaces, or restarting the server.
Step-by-Step
- Find the exact file name in the log. Open the PostgreSQL server log (its location is set by
log_directoryandlogging_collectorinpostgresql.conf). Search for58P03orfile name too long. The log line usually includes the path PostgreSQL tried to create or open. Write that path down exactly; the rest of the fix depends on it. - Measure the path. On Linux or macOS run
echo -n '/the/full/path' | wc -cto count bytes, andecho -n 'component' | wc -cfor the longest single directory or file name. On Windows, check the total character count of the path. Compare against the limits above. If any single component is over 255 bytes, that component is the problem; if the total is over the OS limit, the depth of the directory tree is the problem. - Identify which operation created the name. Common sources: a
pg_basebackuporpg_dumpoutput directory nested too deeply; a tablespace located under a very long path;archive_commandcopying WAL files into a long destination; or a query whose temporary files land in a longtemp_tablespacespath. Match the logged path to one of these. - Shorten the offending path. For a backup or dump, re-run it with a shorter output directory, for example
/backup/baseinstead of a deep project folder. For an archive destination, editarchive_commandinpostgresql.confto write to a shorter directory, then reload the configuration withSELECT pg_reload_conf();. - Move a tablespace if its location is too deep. Create a new directory at a short path, for example
/pgdata/ts1, stop the server, move the tablespace contents there, update thelocationinpg_tablespace_location()'s source (the symbolic link underpg_tblspc), fix ownership and permissions to match the data directory, and start the server. This step changes where data lives, so take a fresh backup first and do it in a maintenance window. - Change temporary file locations. If the error comes from query spills, set
temp_tablespacesto a tablespace on a short path, or pointtemp_file_limit-related work at a shorter directory. Reload or restart as required by the parameter's context. - Re-run the failed operation. Repeat the backup, restore, archive copy, or query. Confirm in the log that no new 58P03 entry appears. If the operation succeeds, no further change is needed.
Common Mistakes
- Looking for a PostgreSQL parameter that raises the limit. There is no
max_file_name_lengthsetting. 58P03 is a server-side check against the OS limit, so the path must be shortened. - Counting characters instead of bytes. On Linux, file names are limited in bytes, so a name with non-ASCII characters can hit the limit sooner than the character count suggests.
- Editing the wrong configuration file. Changes to
archive_commandortemp_tablespacesonly take effect after a reload or restart, and only in thepostgresql.confthe running server actually reads. - Moving a tablespace while the server is running. Always stop the server first, and keep ownership and permissions identical to the original location.
- Assuming the filesystem is at fault. The server can reject a name that the filesystem would accept, so the log message is the authority, not a manual
touchtest.
Tips
- Keep backup, archive, and tablespace directories at short, shallow paths such as
/backupor/pgdata. This prevents most 58P03 cases before they happen. - When scripting backups, build the output path from a fixed short base directory plus a date stamp, not from the current working directory.
- On Windows, enabling long path support is an OS-level change, not a PostgreSQL one; even then, the server's own check may still reject very long names, so shortening the path remains the reliable fix.
- Record the exact path from the log in your incident notes; the same long base directory often causes repeat failures across different operations.
Troubleshooting
If the error persists after shortening the visible path, check for symlinks: a short-looking path can resolve to a long real path. Run readlink -f /the/path on Linux to see the resolved location. If the error occurs during a restore, confirm the restore tool is not recreating the original deep directory structure; specify a short target directory explicitly. If it occurs during WAL archiving, test the archive_command manually as the postgres user to see the exact path it builds. If it occurs during queries, check whether a client-set temp_tablespaces or a per-session setting is overriding the server default. When in doubt, search the full log for every 58P03 entry and fix the longest common base directory first; that usually clears several failures at once. If the server will not start after moving a tablespace, restore the previous location and permissions, start the server, and re-check the path length before trying again.
Frequently Asked Questions
What does PostgreSQL error 58P03 mean exactly?
It is the server's ERRCODE_FILE_NAME_TOO_LONG condition: a file or directory name PostgreSQL tried to use exceeded the operating system's limit, so the operation was refused.
Can I increase a PostgreSQL setting to allow longer file names?
No. There is no server parameter for this. The limit comes from the operating system, so the fix is to shorten the path or file name involved.
Does 58P03 corrupt data?
No. The operation that hit the limit fails, but existing data is not modified by the error itself. Re-run the operation after shortening the path.
Why does it happen only on some servers?
The limit depends on the operating system and filesystem, and on how deeply the data, backup, or archive directories are nested on that host. Two servers running the same PostgreSQL version can behave differently.
Is it safe to move a tablespace to fix this?
It is safe when done with the server stopped, a fresh backup in hand, and ownership and permissions preserved. Treat it as a maintenance-window task.
Where do I find the file name that caused the error?
In the PostgreSQL server log. Search for 58P03 or file name too long; the entry normally includes the path that was rejected.