Unable to delete database of a Cloud SQL instance

Problem

When deleting a database of a Cloud SQL instance running PostgreSQL, you are consistently seeing the error message below:

The attempted action failed, please try again.

Environment

  • Cloud SQL instance
  • PostgreSQL
  • Cloud Console

Solution

  1. Verify the PostgreSQL user who is currently the owner of the database. You can check for the current owner by performing the following steps:
    1. Connect to the PostgreSQL instance with the postgres user account or another administrative user using the following command:
      psql -h IP_ADDRESS_OF_CLOUD_SQL_INSTANCE -U ADMIN_USERNAME
    2. After confirming that the step above was successful, run the following command to list all databases associated with the Cloud SQL instance:
      \l
    3. Verify the creator of the database that you attempted to delete by inspecting the Owner column of the database.
  2. Connect to the PostgreSQL instance with the user account used to create the database using the following command:
    psql -h IP_ADDRESS_OF_CLOUD_SQL_INSTANCE -U OWNER_OF_DB_TO_BE_DELETED -d postgres
  3. Run the following command to delete (drop) the database:
    drop database NAME_OF_DB_TO_BE_DELETED

Cause

When a database is owned by another user, the deletion will fail because the user is missing permission to delete the database.