Search code examples
sqldatabasepostgresqlcreate-tabledbeaver

DBeaver / PostgreSQL: "Error: database already exists", but I can't find it


I want to create database called "President" by rightclicking on PostgreSQL and selecting Create Database.

However, I get the error in the screenprint below.

I can create databases with other names like SomeOtherDatabase and SomeOtherDatabase2 (see screenprints).

Any ideas how I can find and delete the database "President" that seems to exist already?

enter image description here enter image description here

UPDATE!!

If I execute

select * from pg_database 

I get the following result:

enter image description here

So database "President" does seem to exist. (Meanwhile I deleted someOtherDatabase and someOtherDatabase2.)

However, if I execute

drop database President

I get:

enter image description here


Solution

  • You can query catalog view pg_database to check if the database already exists:

    select datname from pg_database WHERE datname = 'president'
    

    And drop it with drop database:

    drop database president;
    

    Note that Postgres' drop database syntax supports the if exists clause, which may come handy in your use case:

    drop database if exists president;