First of all it's \d <table_name> (note the backslash \d not /d), but that's only available in the psql interactive terminal
My [objective] with this program is to get the table structure
You can use the SQL table information_schema.columns which carries the information you want
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = '<table_name>';
column_name | data_type | is_nullable
--------------------+-------------------+-------------
column_a | integer | YES
column_b | boolean | NO
Here's a list of the columns available for use: https://www.postgresql.org/docs/9.4/static/infoschema-columns.html
Snippet
import psycopg2
conn = psycopg2.connect(database='carto', user=..., password=...)
q = """
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = %s;
"""
cur = conn.cursor()
cur.execute(q, ('BADGES_SFR',)) # (table_name,) passed as tuple
cur.fetchall()
# Example Output
[('column_a', 'integer', 'YES'),
('column_b', 'boolean', 'NO'),
...,
]
Tip for Jinja2/Flask integration:
Consider using psycopg2.extras.DictCursor, it'll make it easier for you to pull the information based on the column name (dict key) in your Flask template, because your'll a have a dict that you can access e.g. using row['data_type'], row['column_name'] etc.
First of all it's \d <table_name> (note the backslash \d not /d), but that's only available in the psql interactive terminal
My [objective] with this program is to get the table structure
You can use the SQL table information_schema.columns which carries the information you want
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = '<table_name>';
column_name | data_type | is_nullable
--------------------+-------------------+-------------
column_a | integer | YES
column_b | boolean | NO
Here's a list of the columns available for use: https://www.postgresql.org/docs/9.4/static/infoschema-columns.html
Snippet
import psycopg2
conn = psycopg2.connect(database='carto', user=..., password=...)
q = """
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = %s;
"""
cur = conn.cursor()
cur.execute(q, ('BADGES_SFR',)) # (table_name,) passed as tuple
cur.fetchall()
# Example Output
[('column_a', 'integer', 'YES'),
('column_b', 'boolean', 'NO'),
...,
]
Tip for Jinja2/Flask integration:
Consider using psycopg2.extras.DictCursor, it'll make it easier for you to pull the information based on the column name (dict key) in your Flask template, because your'll a have a dict that you can access e.g. using row['data_type'], row['column_name'] etc.
If you only need the columns description:
>>> cur.execute('select * from t')
>>> description = cur.description
>>> print description
(Column(name='record_timestamptz', type_code=1184, display_size=None, internal_size=8, precision=None, scale=None, null_ok=None),)
>>> columns = description[0]
>>> print columns.name, columns.type_code
record_timestamptz 1184
For the data type name cache the pg_type relation at the application start and make it a dictionary:
cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cursor.execute ('select oid, * from pg_type')
pg_type = dict([(t['oid'], t['typname']) for t in cursor.fetchall()])
cursor.execute ('select * from t')
for t in cursor.description:
print t.name, t.type_code, pg_type[t.type_code]
This did the trick for me:
cursor.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""")
for table in cursor.fetchall():
print(table)
pg_class stores all the required information.
executing the below query will return user defined tables as a tuple in a list
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
cursor.execute("select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';")
print cursor.fetchall()
output:
[('table1',), ('table2',), ('table3',)]
python - How to specify Schema in psycopg2 connection method? - Stack Overflow
python - Get the names of all schemas with Psycopg2 - Stack Overflow
psycopg2 - Postgres DB Schema
python - Getting data from schema in psycopg2 - Stack Overflow
When we were working on ThreadConnectionPool which is in psycopg2 and creating connection pool, this is how we did it.
from psycopg2.pool import ThreadedConnectionPool
db_conn = ThreadedConnectionPool(
minconn=1, maxconn=5,
user="postgres", password="password", database="dbname", host="localhost", port=5432,
options="-c search_path=dbo,public"
)
You see that options key there in params. That's how we did it.
When you execute a query using the cursor from that connection, it will search across those schemas mentioned in options i.e., dbo,public in sequence from left to right.
You may try something like this:
psycopg2.connect(host="localhost",
port="5432",
user="postgres",
password="password",
database="database",
options="-c search_path=dbo,public")
Hope this might help you.
If you are using the string form you need to URL escape the options argument:
postgresql://localhost/airflow?options=-csearch_path%3Ddbo,public
(%3D = URL encoding of =)
This helps if you are using SQLAlchemy for example.
you want to join it against pg_namespace:
t=# select nspname||'.'||relname from pg_class join pg_namespace on relnamespace = pg_namespace.oid where relkind='r' and relname !~ '^(pg_|sql_)' limit 5;
?column?
-------------------
public.s141
public.events
public.tg_rep_que
public.t4
public.menupoint
(5 rows)
select
relname,
oid::regclass as full_name,
relnamespace::regnamespace as schema_name
from pg_class
where relkind='r';
Note that it does not append schema name to tables which available by search_path parameter so you could to use the schema_name column from the query above.
Hi, I need some assistance please. I'm trying to connect to a Postgres database that's hosted on AWS.
The database has multiple schemas. The schema I want to connect to is not the public one.
I connect but cannot execute queries on the none public schemas. I can list all schemas but just cannot query the schema I want. I am using the options tag in the connection string but still it cannot see tables.
When I query anything in public it can see all tables.
Any advice please.
The construction
sql.Identifier('myschema.mytable')
is treated as a single quoted identifier, as can be seen from the produced query. Please see the answer by @gg for how to handle qualified identifiers in Psycopg since version 2.8.
(For older versions) You should pass the schema and table name as separate identifiers to format:
cursor.execute(sql.SQL("SELECT * FROM {}.{}").format(
sql.Identifier('myschema'),
sql.Identifier('mytable'))
Note that the schema and table name must match exactly, case and all, since psycopg2's SQL string composition tools produce quoted identifiers, and quoted identifiers are case-sensitive.
Since version 2.8 (released in Apr 4, 2019), you can pass multiple strings to sql.Identifier to represent a qualified identifier (e.g. a schema name + a table name).
cursor.execute(
sql.SQL("SELECT * FROM {table}").format(
table=sql.Identifier("myschema", "mytable")
)
)
# SELECT * FROM "myschema"."mytable"
See: https://www.psycopg.org/docs/sql.html#psycopg2.sql.Identifier