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 !
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With