Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres drop table syntax error

Tags:

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 
like image 807
Derek Avatar asked Jan 10 '14 18:01

Derek


People also ask

How do I force drop a table in PostgreSQL?

You can remove table(s) from the database in PostgreSQL by using the statement DROP TABLE. It destroys the table with the indexes, rules, triggers, and constraints related to that table. The syntax is as follow: DROP TABLE [IF EXISTS] table_name [CASCADE | RESTRICT];

How do you use drop cascade?

DROP TABLE department CASCADE; If a table is referenced by a view or a foreign key constraint, then use the CASCADE parameter to remove the dependent objects such as views, procedures, but in the case of the foreign key, it will just remove a foreign key constraint from a child table, not the child table itself.

How do I drop a PostgreSQL database?

The first method to remove a PostgreSQL database is to use the following SQL statement: DROP DATABASE <database name>; The command removes the directory containing the database information and the catalog entries. Only the database owner can execute the DROP DATABASE command.

Does DROP TABLE remove constraints?

DROP TABLE always removes any indexes, rules, triggers, and constraints that exist for the target table. However, to drop a table that is referenced by a view or a foreign-key constraint of another table, CASCADE must be specified.


2 Answers

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.

like image 111
Mike Christensen Avatar answered Sep 17 '22 13:09

Mike Christensen


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.

like image 24
Mauricio Marin Avatar answered Sep 18 '22 13:09

Mauricio Marin