Meta commands in Psycopg2 - \ d do not work

I want to list all the column names of a table using the psycopg2 Python package (2.7). But I can not execute the following request -

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

Is there an alternative to how I can list table column names with psycopg2 ? Please indicate any duplicates. Thanks!

+5
source share
1 answer

The psql command line has several shortcuts, such as \d , but is not part of SQL. You need to request information_schema :

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

EDIT: This is really important information that the psql -E command line will display SQL queries used to implement \d and other backslash commands (whenever you use one of them on the psql command line) like @ piro wrote in a comment. Thus, you get what you want very easily.
Thanks @piro!

+5
source

All Articles