First find your database oid by using system catalog.
SELECT * FROM pg_database;
Then use format function to write drop statement with escape characters.
SELECT format('DROP DATABASE %I', datname)
FROM pg_database
WHERE oid = your_dbs_oid_here;
The output can be run as SQL statement.
With psql
, you can use \gexec
to avoid the copy and paste of the statement.
Since it's a risky drop command first run and see the output
postgres=# select oid, datname from pg_database where oid = 74304;
oid | datname
-------+------------------------------------------------------------
74304 | + \r�💡'"><&;|${${lower:j}${::-n}d${upper:ı}:dns${::-:}//h
(1 row)
postgres=# SELECT format('DROP DATABASE %I', datname)
FROM pg_database
WHERE oid = 74304;
format
-----------------------------------------------------------------------------
DROP DATABASE "+ \r�💡'""><&;|${${lower:j}${::-n}d${upper:ı}:dns${::-:}//h"
(1 row)
Then, without executing other command, execute \gexec
command to make it happen.
postgres=# \gexec
DROP DATABASE