You can't do it directly as one can not write a 'normal' query if the table names are not known (ie. coming from some variable or subquery). But you can build and execute a dynamic SQL statement for this. For example, if you need the column 'name' from every table, you can do the following (inside a PL/pgSQL function):

FOR i IN SELECT table_name 
           FROM information_schema.tables 
          WHERE table_schema = 'your_desired_schema'
LOOP
    sql_string := sql_string || format($$
        -- some whitespace is mandatory here
        UNION
        SELECT name FROM %I
    $$,
    i.table_name);
END LOOP;

EXECUTE sql_string;

In this form it won't work however, since you cannot SELECT in plpqsql unless you do it into a variable. You can either create a temporary table for this, loop over the results (in an other FOR loop), or - not using UNION - just return in every iteration, depending on your needs.

And, of course, this presumes that you want to select a single column (or more than one, but always with the same name and type) from all the tables. If you simply need all the data from every table, then the tables must have the same structure (the same column types in the same order, with the same names).

Notes:

  • the format() function was introduced in version 9.1
  • I've omitted some mandatory elements of any PL/pgSQL block to keep it simple
  • the earlier version mentioned using a DO block. The problem with it is that you cannot simply use SELECT there to return rows, as it was pointed out in another answer.
Answer from András Váczi on Stack Exchange
🌐
Neon
neon.com › postgresql › tutorial › select
PostgreSQL SELECT
This tutorial shows you how to use the basic PostgreSQL SELECT statement to retrieve data from a single table.
🌐
CommandPrompt Inc.
commandprompt.com › education › how-to-select-all-from-a-table-in-postgresql
How to Select All From a Table in PostgreSQL — CommandPrompt Inc.
March 2, 2023 - The SELECT statement is executed with the “*” symbol to select all the data from a particular Postgres table. The ORDER BY clause can be used with the SELECT * command to sort the result set in a particular order.
Address   2950 Newmarket ST STE 101 - 231, 98226, Bellingham
🌐
PostgreSQL
postgresql.org › docs › current › tutorial-select.html
PostgreSQL: Documentation: 18: 2.5. Querying a Table
May 14, 2026 - Here * is a shorthand for “all columns”. [2] So the same result would be had with: SELECT city, temp_lo, temp_hi, prcp, date FROM weather; ... city | temp_lo | temp_hi | prcp | date ---------------+---------+---------+------+------------ San Francisco | 46 | 50 | 0.25 | 1994-11-27 San Francisco | 43 | 57 | 0 | 1994-11-29 Hayward | 37 | 54 | | 1994-11-29 (3 rows) You can write expressions, not just simple column references, in the select list.
Top answer
1 of 3
5

You can't do it directly as one can not write a 'normal' query if the table names are not known (ie. coming from some variable or subquery). But you can build and execute a dynamic SQL statement for this. For example, if you need the column 'name' from every table, you can do the following (inside a PL/pgSQL function):

FOR i IN SELECT table_name 
           FROM information_schema.tables 
          WHERE table_schema = 'your_desired_schema'
LOOP
    sql_string := sql_string || format($$
        -- some whitespace is mandatory here
        UNION
        SELECT name FROM %I
    $$,
    i.table_name);
END LOOP;

EXECUTE sql_string;

In this form it won't work however, since you cannot SELECT in plpqsql unless you do it into a variable. You can either create a temporary table for this, loop over the results (in an other FOR loop), or - not using UNION - just return in every iteration, depending on your needs.

And, of course, this presumes that you want to select a single column (or more than one, but always with the same name and type) from all the tables. If you simply need all the data from every table, then the tables must have the same structure (the same column types in the same order, with the same names).

Notes:

  • the format() function was introduced in version 9.1
  • I've omitted some mandatory elements of any PL/pgSQL block to keep it simple
  • the earlier version mentioned using a DO block. The problem with it is that you cannot simply use SELECT there to return rows, as it was pointed out in another answer.
2 of 3
2

This is a workaround based on the above suggested approach.

 DECLARE
 tables CURSOR FOR SELECT * 
      FROM information_schema.tables 
      WHERE table_schema = 'public' 
      ORDER BY "table_name" ASC
      LIMIT ((SELECT count(*)
          FROM information_schema.tables
          WHERE table_schema = 'public')-1);
          --Because the following prepared string starts with a 'UNION ALL',
          --this completes the query string with a select starting with the last table
          sql_string text := 'SELECT field1, field7, field8, "source" FROM ' || quote_IDENT((SELECT "table_name" FROM information_schema.tables WHERE table_schema = 'public' ORDER BY "table_name" DESC LIMIT 1));

 BEGIN

 FOR table_record IN tables LOOP
     sql_string := sql_string || '
         UNION ALL
         SELECT columns FROM ' || quote_IDENT(table_record."table_name");
 END LOOP;

 sql_string := sql_string||';';
 RETURN sql_string;
 --EXECUTE sql_string;

 END;
🌐
W3Schools
w3schools.com › postgresql › postgresql_select.php
PostgreSQL - Select Data
PostgreSQL Operators PostgreSQL SELECT PostgreSQL SELECT DISTINCT PostgreSQL WHERE PostgreSQL ORDER BY PostgreSQL LIMIT PostgreSQL MIN and MAX PostgreSQL COUNT PostgreSQL SUM PostgreSQL AVG PostgreSQL LIKE PostgreSQL IN PostgreSQL BETWEEN PostgreSQL AS PostgreSQL Joins PostgreSQL INNER JOIN PostgreSQL LEFT JOIN PostgreSQL RIGHT JOIN PostgreSQL FULL JOIN PostgreSQL CROSS JOIN PostgreSQL UNION PostgreSQL GROUP BY PostgreSQL HAVING PostgreSQL EXISTS PostgreSQL ANY PostgreSQL ALL PostgreSQL CASE ... To retrieve data from a database, we use the SELECT statement.
🌐
PostgreSQL Tutorial
pgtutorial.com › home › postgresql tutorial › postgresql select: querying data from a table
PostgreSQL SELECT Statement: Retrieving Data from a Table
January 1, 2025 - SELECT column1, column2, column3 FROM table_name;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql) Or you can use SELECT * to retrieve data from all the column of the table:
🌐
PostgreSQL
postgresql.org › docs › current › sql-select.html
PostgreSQL: Documentation: 18: SELECT
May 14, 2026 - SELECT retrieves rows from zero or more tables. The general processing of SELECT is as follows: All queries in the WITH list are computed. These effectively serve as temporary tables that can be referenced in the FROM list. A WITH query that is referenced more than once in FROM is computed ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › postgresql › postgresql-show-tables
PostgreSQL - Show Tables - GeeksforGeeks
July 15, 2025 - SELECT * FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'; In this example, we will query for the list of all tables in the sample database, ie, dvdrental.
🌐
Peterbe.com
peterbe.com › plog › select-all-relations-in-postgresql
Select all relations in PostgreSQL - Peterbe.com
December 10, 2015 - peterbecom=# \d ********* QUERY ********** SELECT n.nspname as "Schema", c.relname as "Name", CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as "Type", pg_catalog.pg_get_userbyid(c.relowner) as "Owner" FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r','v','m','S','f','') AND n.nspname <> 'pg_catalog' AND n.nspname <> 'information_schema' AND n.nspname !~ '^pg_toast' AND pg_catalog.pg_table_is_visible(c.oid) ORDER BY 1,2; ************************** With this I was able to come up with this SQL select to get all tables, views, sequences and functions.
🌐
Neon
neon.com › postgresql › administration › show-tables
PostgreSQL Show Tables
Use the \dt or \dt+ command in psql to show tables in a specific database. Use the SELECT statement to query table information from the pg_catalog.pg_tables catalog.
🌐
Hevo
hevodata.com › home › learn › database management system
How to Use the PostgreSQL SELECT Statement?[Syntax+Examples]
January 10, 2026 - Very useful when you need a full view of the table’s data. ... Fetches only rows with specific conditions. This helps narrow the data you need in the system; for example, all employees with salaries above a certain amount. SELECT column1, column2 FROM table_name WHERE condition;
🌐
DbSchema
dbschema.com › blog › postgresql › show tables in postgresql – psql, sql queries, and schema filters | dbschema
Show Tables in PostgreSQL – psql, SQL Queries, and Schema Filters | DbSchema
April 14, 2020 - The PostgreSQL equivalent of SHOW ... in psql, or an information_schema query in any SQL client: ... SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE' ORDER BY table_name; Read on for schema filters, size information, and a full comparison of all available ...
🌐
GeeksforGeeks
geeksforgeeks.org › postgresql › postgresql-select
PostgreSQL - SELECT - GeeksforGeeks
June 11, 2026 - SELECT column1, column2, ... FROM table_name [WHERE condition] [GROUP BY column_name] [ORDER BY column_name] [LIMIT number]; Prerequisites: Assume the following table is available: CREATE TABLE employees ( id SERIAL PRIMARY KEY, name VARCHAR(50), age INT, department VARCHAR(50), salary NUMERIC ); To retrieve all columns from the employees table, you can use the * wildcard:
🌐
Neon
neon.com › postgresql › postgresql-tutorial › postgresql-inner-join
PostgreSQL INNER JOIN
January 17, 2024 - SELECT select_list FROM table1 t1 INNER JOIN table2 t2 USING(column_name); For each row in the table1, the inner join compares the value in the column_name with the value in the corresponding column of every row in the table2.
🌐
CommandPrompt Inc.
commandprompt.com › education › how-to-use-select-query-in-postgresql
How to Use Select Query in PostgreSQL — CommandPrompt Inc.
August 1, 2022 - Follow the below syntax to fetch the record of all columns in PostgreSQL: ... Specifying an asterisk * with the SELECT statement will provide the data of all columns. Example: How to Fetch All Columns Data in PostgreSQL?
🌐
YouTube
youtube.com › watch
How to Efficiently Select All Rows from Multiple Tables in PostgreSQL - YouTube
Learn how to efficiently select data from multiple tables in a PostgreSQL schema, focusing on rows where the 'sqft' column is NULL.---This video is based on ...
Published   March 30, 2025
Views   3
🌐
Sentry
sentry.io › sentry answers › postgres › how to show tables in postgresql?
How to show tables in PostgreSQL? | Sentry
The following terminal command (-c) will display all tables in the db database without having to type in psql. ... If you are using a general SQL app instead of psql, the following command will work. It will work in psql too. SELECT * FROM pg_catalog.pg_tables WHERE schemaname='public';