Эти команды не для sql специфичны для psql - PostgreSQL интерактивный терминал . Вы не можете выполнить их с помощью psycopg2, но вы можете запустить psql из своего приложения. Приведенный ниже полный пример в Python 3.6 использует подпроцесс :
import psycopg2
import sys
import subprocess
conn = psycopg2.connect(host='localhost', dbname='test', user='postgres')
conn.autocommit = True
cur = conn.cursor()
cur.execute('create table my_table(id int primary key, str text)')
res = subprocess.run('psql -c "\d+ my_table" test postgres', stdout=subprocess.PIPE)
print(res.stdout.decode(sys.stdout.encoding))
Выход:
Table "public.my_table"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+----------+--------------+-------------
id | integer | | not null | | plain | |
str | text | | | | extended | |
Indexes:
"my_table_pkey" PRIMARY KEY, btree (id)