Discussion:
PSQL command to drop table from a databae
Shreerang Patwardhan
2010-03-28 04:11:06 UTC
Permalink
Hey all,
I am trying the following command to drop a table from a postgresql
database. It gives an error. Can anyone please rectify the command if it is
wrong?

*Command:*
psql -h localhost -p 5433 -U postgres -d testdb -c DROP TABLE
11001_district_of_columbia_arealm

*Error Message:*
psql: warning: extra command-line argument "TABLE" ignored
psql: warning: extra command-line argument
"11001_district_of_columbia_arealm" ignored
ERROR: syntax error at end of input
LINE 1: DROP
^


Please help!
Thanx,
Shreerang Patwardhan.
Maciej Sawicki
2010-03-28 04:32:48 UTC
Permalink
Hi,
I think it should be:
psql -h localhost -p 5433 -U postgres -d testdb -c "DROP TABLE
11001_district_of_columbia_arealm"

regards,
Maciej Sawicki
Shreerang Patwardhan
2010-03-28 10:10:21 UTC
Permalink
Hello Maciej,
I have tried up the quotes as well...Still doesn't work....[?]

Do you think that 11001_district_of_columbia_arealm is an invalid table
name? Actually, a table with the said name can be created using psql command
as well as through pgadmin3. But then why does it give an error while
deleting this table? The error message reads as:

ERROR: syntax error at or near "11001"
LINE 1: DROP TABLE 11001_district_of_columbia_arealm
^

I am unable to guage what the problem is?...Please help me....

Thanx,
Shreerang Patwardhan.
Olivier Courtin
2010-03-28 12:13:25 UTC
Permalink
Post by Shreerang Patwardhan
Do you think that 11001_district_of_columbia_arealm is an invalid
table name?
Yes, label table name first char cannot be a number.
Post by Shreerang Patwardhan
Actually, a table with the said name can be created using psql command
Really ?

postgres=# CREATE TABLE 1a();
ERROR: syntax error at or near "1"

with psql (PostgreSQL) 8.3.7



P.S: These kind of question is more PostgreSQL than PostGIS relevant

--
Olivier
Olivier Courtin
2010-03-28 12:40:07 UTC
Permalink
Post by Olivier Courtin
Post by Shreerang Patwardhan
Actually, a table with the said name can be created using psql command
Really ?
Well in fact, my previous answer wasn't right at all

CREATE TABLE "1a" ();
and so
DROP TABLE "1a";

works both well...

So just need to double quote the label name

--
Olivier
Shreerang Patwardhan
2010-03-28 15:35:41 UTC
Permalink
Hi Ollvier,
I was just writing to you about your previous mail. Well, th e DROP TABLE
command is not working at my end. I have used double quotes also. Can you
guess what could the problem be? I am trying to use that command in a shell
script and it is not working. I have also tried it on the terminal and still
the result is the same....ERROR!!!.....[?]
Need help!!!
Kevin Neufeld
2010-03-28 16:25:12 UTC
Permalink
Since you are using a non-standard way of naming your table (starting
with number), as Oliver was explaining, you need to double quote your
table name.

$ set mytab=\"123foo\"

$ echo $mytab
"foo"

$ psql -c "CREATE TABLE $mytab( a int ) " postgis
CREATE TABLE

$ psql -c "\dt" postgis
List of relations
Schema | Name | Type | Owner
--------+------------------+-------+----------
public | 123foo | table | kneufeld
public | geometry_columns | table | kneufeld
public | spatial_ref_sys | table | kneufeld
(3 rows)

$ psql -c "DROP TABLE $mytab" postgis
DROP TABLE

-- Kevin
Post by Shreerang Patwardhan
Hi Ollvier,
I was just writing to you about your previous mail. Well, th e DROP
TABLE command is not working at my end. I have used double quotes
also. Can you guess what could the problem be? I am trying to use that
command in a shell script and it is not working. I have also tried it
on the terminal and still the result is the same....ERROR!!!.....
Need help!!!
_______________________________________________
postgis-users mailing list
http://postgis.refractions.net/mailman/listinfo/postgis-users
Shreerang Patwardhan
2010-03-29 09:48:53 UTC
Permalink
Hey Kevin,
Thanx a million tons.....My problem is solved...Thanx a lot.....!!!....
Loading...