How do you use slashes in commances psycopg2?
-
Subject: We need to work on the postgres decryptic after we've been on the team. Through psql, the team's going through, it's normal. And when I want to do the same thing in psycopg2, it doesn't work.
Input:
cursor.execute('\dRs')
Output:
psycopg2.errors.SyntaxError: syntax error at or near "\"
P.S. r'dRs doesn't work either. ♪ ♪
-
If you need an interactive team from the violin, you'll have to start a base program.
psql
and command\dRs
after counting the resulthttps://stackoverflow.com/a/53087565/5909792 :
import sys import subprocess
import psycopg2
conn = psycopg2.connect(host='localhost', dbname='test', user='postgres')
conn.autocommit = Truecur = 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))
Result:
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)