Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meta commands in Psycopg2 - \d not working

I wish to list all the column names of a table using psycopg2 package of Python (2.7). But I am unable to execute the following query -

cur.execute("\d my_table");
psycopg2.ProgrammingError: syntax error at or near "\"

Is there an alternate as to how I can list the column names of a table using psycopg2 ? Please point out any duplicates. Thanks !

like image 963
Utsav T Avatar asked Jul 21 '15 07:07

Utsav T


1 Answers

Command line psql has some shortcuts like \d but it's not part of SQL. What you need is to query information_schema:

SELECT column_name FROM information_schema.columns WHERE table_name = 'my_table';

EDIT: It's really an important information that the command line psql -E will echo SQL queries used to implement \d and other backslash commands (whenever you use one of them in the psql prompt) as @piro has written in comment. This way you get what you want very easily.
Thanks @piro!

like image 64
ElmoVanKielmo Avatar answered Nov 06 '22 02:11

ElmoVanKielmo