29

Postgres 9.3.2 on heroku.

Pretty sure I'm just being an idiot, but I can't seem to figure out why my syntax is wrong.

db=> \dt
              List of relations
 Schema |    Name    | Type  |     Owner      
--------+------------+-------+----------------
 public | device     | table | admin
 public | post       | table | admin
 public | user       | table | admin
(3 rows)

// why does this fail?
db=> drop table user; 
ERROR:  syntax error at or near "user"
LINE 1: drop table user;

// does the right thing
db=> drop table error; 
ERROR:  table "error" does not exist
Derek
  • 11,980
  • 26
  • 103
  • 162

2 Answers2

48

User is a reserved keyword in Postgres. You'll have to put it in quotes if you want to refer to an actual table named user:

DROP TABLE "user";

Probably best to stay away from using reserved keywords as table names if you can help it. It usually ends up creating weird problems down the road. Users might be a better name for a table.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
5

I had the same error. My database name was very unique and not a reserved keyword. Still needed to wrap the database name with quotation marks

"<database_name>"

Also for those that might forget always add a semicolon ; at the end of the statement, I always forget.