简体   繁体   中英

Get the names of all schemas with Psycopg2

I'm trying to iterate all tables of a Postgresql DB in Python, to execute a query in each of them. I need to get the schema and table name of each of them in order to execute the queries, so I'd like to get a list of strings like this: "schema.tablename".

I've used this, but it only lists the names of tables, so it is of no help.

select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';

I'm sure there must be a more simple way, but I have been looking for something with no success...

Any help appreciated, thanks!!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM