Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 57P04: Database Dropped Explained

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Error 57P04 means the database you are trying to connect to no longer exists on the PostgreSQL server. The fix is to reconnect to a database that still exists, then either restore the dropped database from a backup or recreate it and reload its schema and data. Before you do anything, confirm the database is actually gone and find out who or what dropped it, because a dropped database cannot be recovered from the running server itself.

What You'll Need

  • Access to the PostgreSQL server as a superuser or a role with CREATEDB and CREATEROLE privileges.
  • The psql client or another PostgreSQL client such as pgAdmin, installed on your machine or available on the server.
  • A recent logical backup (pg_dump or pg_dumpall) or a physical backup/base backup of the database, if you need to recover data.
  • Knowledge of the database name that is failing and the connection string or client configuration that points to it.
  • Permission to check server logs, which usually requires shell access to the server or access to the logging system.

Step-by-Step

  1. Confirm the error and the database name. The message usually reads something like FATAL: database "mydb" does not exist or includes SQLSTATE 57P04. Note the exact database name from the error, because a typo in a connection string produces the same symptom.
  2. Connect to a database that still exists. Use the default maintenance database, usually postgres, or the template1 database. For example: psql -U postgres -d postgres. If your client is configured to connect to the missing database automatically, override the database name explicitly.
  3. List the databases on the server. Run \l in psql or SELECT datname FROM pg_database;. If the database is absent from the list, it has been dropped or you are connected to a different server or cluster than you think.
  4. Check whether you are on the right server and port. A common cause is connecting to a staging or test instance instead of production. Run SELECT current_database(), inet_server_addr(), inet_server_port(); and compare with the expected host and port.
  5. Find out when and how it was dropped. Check the PostgreSQL server log for lines containing DROP DATABASE or the database name around the time the error started. If you use an audit extension or a managed service with an activity log, review that too. This tells you whether the drop was intentional, accidental, or part of a script.
  6. Stop anything that recreates the connection. If an application, cron job, or connection pooler keeps retrying, pause it so you can work without noise. Fixing the database will not help if a deployment script drops it again on every run.
  7. Restore from backup if you need the data. Create an empty database first with CREATE DATABASE mydb OWNER myuser;, then restore the dump: psql -U postgres -d mydb -f mydb_backup.sql for a plain SQL dump, or pg_restore -U postgres -d mydb mydb_backup.dump for a custom-format dump. If you use a physical backup, follow your provider's or your own documented restore procedure instead of restoring files by hand.
  8. Recreate the database from scratch if no backup exists. Run CREATE DATABASE mydb OWNER myuser;, then recreate roles, extensions, schemas, tables, indexes, and grants from your migration scripts or schema definition. Reload any seed or reference data you have.
  9. Update connection settings. Point your application, ORM, and any connection pooler at the correct database name, host, and port. If the database was renamed rather than dropped, update the configuration to the new name instead of recreating it.
  10. Verify the fix. Connect with the same client and credentials the application uses: psql -U myuser -h myhost -d mydb -c 'SELECT 1;'. Then start the application and confirm it connects without error 57P04.

Common Mistakes

  • Assuming the data can be recovered from the live server. Once a database is dropped, its files are removed; only a backup or a replica can bring the data back.
  • Recreating the database with the wrong owner or encoding. Match the original owner, encoding, locale, and template settings where possible, or permissions and collation behavior may change.
  • Restoring into the wrong database. Always create and target the intended database explicitly; restoring into postgres or template1 causes confusion and can break the cluster's defaults.
  • Ignoring the cause. If a script, migration, or scheduled job dropped the database, it will do so again. Find and fix the source before restoring.
  • Forgetting dependent objects. Roles, extensions, and grants that lived outside the database may also need to be recreated.
  • Connecting to the wrong host or port. Many 57P04 reports are simply a misconfigured connection string pointing at a different server.

Tips

  • Keep regular logical backups with pg_dump and test restoring them, not just creating them.
  • Restrict DROP DATABASE privileges to a small set of roles, and avoid granting it to application users.
  • Use connection poolers and application configs that read the database name from one place so a rename or restore only needs one change.
  • Enable logging of DDL statements if your setup allows it, so drops and other schema changes are traceable.
  • On managed PostgreSQL services, check whether point-in-time recovery or automated backups are available before recreating anything by hand.

Troubleshooting

If the database still appears in \l but clients get 57P04, check that clients are connecting to the same host, port, and cluster. A load balancer or DNS entry may point to an old replica. If the database exists but the error persists, look for a stale connection string cached by the application or pooler and restart that component.

If CREATE DATABASE fails, check that your role has CREATEDB and that the name is not already taken. If restore fails partway, read the first error rather than the last; missing roles or extensions are common causes. Create those first, then rerun the restore. If you cannot find a backup, check replicas, snapshots, and any export files before accepting data loss. If the drop was caused by a person or script, document it and adjust permissions so it cannot happen again.

Frequently Asked Questions

Can a dropped PostgreSQL database be recovered without a backup?

Not from the running server. The database files are removed when it is dropped. Recovery requires a backup, a replica, a snapshot, or point-in-time recovery if your setup provides it.

What does SQLSTATE 57P04 mean exactly?

It is the error code for "database dropped," raised when a session or connection refers to a database that no longer exists on the server.

Does dropping a database also drop its roles and extensions?

Roles are cluster-wide and are not dropped with the database. Extensions installed inside the database are removed with it and must be recreated after restore or recreation.

Why do I get 57P04 when the database name looks correct?

Usually the client is connecting to a different server, port, or cluster than expected, or a pooler has cached an old connection target. Verify the host and port with a direct connection.

Can I prevent accidental drops?

Yes. Limit DROP DATABASE to trusted roles, avoid giving it to application users, enable DDL logging where possible, and keep tested backups.

Is 57P04 the same as "database does not exist"?

They are closely related symptoms. A missing database name in the connection string produces a similar message, so confirm the name and the server before assuming a drop.