raycarnes
raycarnes

Reputation: 63

How do you drop a PostgreSQL database with a carriage return in the name?

Somehow, via a restore done through code I ended up with a database with the following name (simulated output from \l command)

       Name       |
------------------|
 \r              +|
 DATABASE_NAME    |

I would like to be able to drop it, but I have no idea how to construct the name properly to include the carriage return when specified via DROPDB or DROP DATABASE commands.

If I can't drop it - can I change the owner to hide it - doesn't that also involve specifying the name?

PostgreSQL 9.2.4 on Ubuntu

Upvotes: 6

Views: 2244

Answers (2)

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125214

do $$
begin
    execute format('drop database %I', E'\rDATABASE_NAME');
end;
$$

Rahul's answer should be

drop database U&"\000ADATABASE_NAME";

Upvotes: 2

Rahul
Rahul

Reputation: 77866

Use unicode for line feed while dropping the database like below

drop database U&"\000ADATABASE_NAME";

You can as well ALTER the name like

alter database U&"\000ADATABASE_NAME" rename to "DATABASE_NAME_NEW";

Upvotes: 3

Related Questions