How to Drop/Create Database name that has upper-case letter?

Vivek S. picture Vivek S. · May 28, 2014 · Viewed 17.1k times · Source

for example my Postgresql database name is Ajp

i cannot drop/create the db with this name ie. Ajp

my scripts for drop is

cd /D D:\\Apps
cd RSTAR PGSQL DOTCONNECT
cd bin
cd Debug
cd PG
psql -U postgres  -d postgres -c "DROP DATABASE  Ajp "

while executing this i got this error

D:\Apps\RSTAR PGSQL DOTCONNECT\bin\Debug\PG>psql -U postgres -d postgres -c "DR OP DATABASE Ajp " ERROR: database "ajp" does not exist

i tried psql -U postgres -d postgres -c "DROP DATABASE 'Ajp' " (db name within quotes)

again got error

ERROR: syntax error at or near "'Ajp'" LINE 1: DROP DATABASE 'Ajp'

how fix this problem ?? (please don't comment that change your dbname to lower-case)

using : "PostgreSQL 9.2.4, compiled by Visual C++ build 1600, 32-bit"

the solution for this problem is below

  • Sholud use escape character \
  • psql -U postgres -d postgres -c "DROP DATABASE \"Ajp\";"

    see the below comment

Answer

a_horse_with_no_name picture a_horse_with_no_name · May 28, 2014

SQL identifiers that are case-sensitive need to be enclosed in double quotes:

DROP DATABASE  "Ajp";

Not sure if they are properly preserved on Windows if you pass them through the commandline though.

You might need to put that into a SQL script and pass the script name to psql.

For details on valid identifiers please see the manual: http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS