Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 649
Vivek S. Avatar asked May 28 '14 07:05

Vivek S.


1 Answers

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

like image 146
a_horse_with_no_name Avatar answered Nov 21 '22 02:11

a_horse_with_no_name