SELECT
    column_name,
    data_type
FROM
    information_schema.columns
WHERE
    table_name = 'table_name';

with the above query you can retrieve columns and its datatype.

Answer from selva on Stack Overflow
🌐
Coefficient
coefficient.io › home
PostgreSQL Get Column Names: A Step-by-Step Guide
December 18, 2025 - To fetch the column names, we use the cur.execute() method to execute a SELECT * FROM query on the target table, but with a LIMIT 0 clause to prevent the actual retrieval of any data.
People also ask

Does Coefficient work with both Google Sheets and Excel?
Yes, Coefficient supports both Google Sheets and Microsoft Excel. You can install Coefficient from the Google Workspace Marketplace for Sheets or from Microsoft AppSource for Excel.
🌐
coefficient.io
coefficient.io › home
PostgreSQL Get Column Names: A Step-by-Step Guide
Can I export data back to my business systems with Coefficient?
Yes, Coefficient supports two-way data sync. You can not only import live data from your business systems but also export updated data back to them. Supported export actions include UPDATE (modify existing records), INSERT (create new records), UPSERT (update or insert), and DELETE operations. This works with systems like Salesforce, HubSpot, QuickBooks, Snowflake, and databases.
🌐
coefficient.io
coefficient.io › home
PostgreSQL Get Column Names: A Step-by-Step Guide
How does Coefficient's automated refresh work?
Coefficient allows you to schedule automatic data refreshes at intervals you choose: hourly (1, 2, 4, or 8 hours), daily, weekly, or monthly. Once scheduled, your spreadsheet data automatically updates from your connected systems without any manual work. You can also trigger manual refreshes anytime with an on-sheet button or through the sidebar.
🌐
coefficient.io
coefficient.io › home
PostgreSQL Get Column Names: A Step-by-Step Guide
Top answer
1 of 4
3

I don't know why you would ever need to do this, but it is possible using some of the JSON functions introduced in 9.3.

SELECT json_object_keys(row_to_json(t)) FROM
 (SELECT * FROM table1
  LEFT JOIN table2 ON table1.id = table2.tbl1_id LIMIT 1) t;

This will give you the name of every column returned for a single row. Without the LIMIT you would get the columns repeated for every row returned. If you wanted to see the values returned as well you can get more complex:

WITH t as
  (SELECT * FROM table1
   LEFT JOIN table2 ON table1.id = table2.tbl1_id LIMIT 1)
SELECT json_data.key, json_data.value
FROM t, json_each_text(row_to_json(t)) AS json_data;

Both these queries will return all the columns even if they are named the same. If all you want is a list of unique column names, you can utilize hstore:

CREATE EXTENSION hstore; --Create the extension if you need it.

SELECT akeys(hstore(t)) as array_of_columns
FROM
(SELECT * FROM table1
 LEFT JOIN table2 ON table1.id = table2.tbl1id LIMIT 1) t;
2 of 4
1

The only reasonable approach I can come up with is to do something like this. Schema location plays into this, but I'm just posting the basic idea. SQL doesn't have reflection at run time unless you go and hit meta data tables, which is what you seem to be trying to achieve here.

SELECT table1.* 
INTO x
      FROM table1 
           LEFT JOIN table2 
                     ON table1.id = table2.tbl1_id LIMIT 0;

select *
from information_schema.columns
where table_Name = 'x';

Also interesting enough while traveling the Internets over lunch I ran into this. Works with PostgreSQL 8.2.0 or higher.

https://github.com/theory/colnames/tree/master/test/sql

🌐
Delft Stack
delftstack.com › home › howto › postgres › get column names in postgresql
How to Get Column Names in PostgreSQL | Delft Stack
March 11, 2025 - Whether you’re a developer, a ... there are two primary methods to get column names: using the \d+ command in the psql command-line interface and executing a SQL query....
🌐
PostgreSQL
postgresql.org › message-id › AANLkTilsjTAXyN5DghR3M2U4c8w48UVxhov4-8igMpd1@mail.gmail.com
PostgreSQL: Re: [SQL] How to Get Column Names from the Table
July 7, 2010 - Hi, To get column names only select column_name from information_schema.columns where table_name='captor_prime_aggregates'; Thanks Sreelatha On Wed, Jul 7, 2010 at …
🌐
TablePlus
tableplus.com › blog › 2018 › 04 › postgresql-how-to-list-all-available-columns.html
PostgreSQL - How to list all columns? | TablePlus
April 9, 2018 - SELECT * FROM information_schema.columns WHERE table_schema = 'schema_name' AND table_name = 'table_name'; ... In TablePlus, you can be able to see all columns from the Postgres GUI with a spreadsheet-like view. From the data table, you can see columns with data: Or from the database structure, ...
Find elsewhere
🌐
Dataedo
dataedo.com › kb › query › postgresql › find-tables-with-specific-column-name
Find tables with specific column name in PostgreSQL database - PostgreSQL Data Dictionary Queries
November 5, 2018 - select t.table_schema, t.table_name from information_schema.tables t inner join information_schema.columns c on c.table_name = t.table_name and c.table_schema = t.table_schema where c.column_name = 'last_name' and t.table_schema not in ...
🌐
PostgreSQL
postgresql.org › docs › 8.4 › queries-select-lists.html
PostgreSQL: Documentation: 8.4: Select Lists
July 24, 2014 - The simplest kind of select list ... (as defined in Section 4.2). For instance, it could be a list of column names: SELECT a, b, c FROM ......
🌐
GeeksforGeeks
geeksforgeeks.org › python › get-column-names-from-postgresql-table-using-psycopg2
Get column names from PostgreSQL table using Psycopg2 - GeeksforGeeks
July 23, 2025 - First, we connect the PostgreSQL database using psycopg2.connect() method, then we create a cursor using cursor() method, after that we use the cursor() then extract the first element from it using slicing.
🌐
PostgreSQL Extension Network
pgxn.org › dist › colnames
colnames: Lists the column names in a PostgreSQL RECORD value / PostgreSQL Extension Network
This extension contains a single SQL function, colnames(), that takes a record value as its argument and returns an array of the names of the columns in that record:
🌐
Postgres Professional
postgrespro.com › list › thread-id › 1463718
Thread: Query to get column-names in table via PG tables? : Postgres Professional
Ken Johanson wrote: >>> I am looking for expertise on how to program the equivalent to this >>> query, but using the pg_catalog tables, which I understand have fewer >>> security restrictions than information_schema in some cases: >>> >>> SELECT column_name >>> FROM information_schema.columns >>> WHERE table_catalog=? AND table_schema=? AND table_name=? >>> ORDER BY ordinal_position >> >> Do what psql does...launch it with psql -E, and it will echo any >> internal queries it makes back to you.
🌐
Stack Overflow
stackoverflow.com › questions › 57375761 › getting-list-of-column-names-in-a-table-postgresql
Getting list of column names in a table (PostgreSQL) - Stack Overflow
I have searched online for some solutions, but none give expected results. Here is what I have tried. select column_name from information_schema.columns where table_name = 'Accounts';
🌐
Baeldung
baeldung.com › home › sql queries › how to retrieve column names from a table in sql
How to Retrieve Column Names From a Table in SQL Baeldung on SQL
August 4, 2024 - In this section, we check out various methods for retrieving column names from a table in PostgreSQL. To begin with, the INFORMATION_SCHEMA view provides metadata about the columns of all tables. By querying this view, we can retrieve the column names for a specific table.
Top answer
1 of 5
39

Information schema vs. system catalogs

The information schema serves certain purposes. System catalogs are the actual source of truth in Postgres.

The information schema provides standardized views which help with portability - mostly across major Postgres versions as portability across different RDBMS platforms typically is an illusion once your queries are sophisticated enough to look up system catalogs. Notably, major RDBMS like Oracle, SQLite, IBM Db2, SAP ASE, or IBM Informix still don't support the information schema (as of June 2026).

Views in the information schema must jump through many hoops to achieve a format complying to the standard. This makes them slow, sometimes very slow. Compare plans and performance for these basic objects:

EXPLAIN ANALYZE SELECT * from information_schema.columns;
EXPLAIN ANALYZE SELECT * from pg_catalog.pg_attribute;

The difference is remarkable.

Your example

For your example SELECT * from tbl compare the two queries below for this simple table:

CREATE TEMP TABLE foo(
   a numeric(12,3)
 , b timestamp(0)
);

Using pg_attribute:

SELECT attname, format_type(atttypid, atttypmod) AS type
FROM   pg_attribute
WHERE  attrelid = 'foo'::regclass
AND    attnum > 0
AND    NOT attisdropped
ORDER  BY attnum;

format_type() returns the complete type with all modifiers:

attname | type
--------+-------------------------------
a       | numeric(12,3)
b       | timestamp(0) without time zone

Also note that the cast to regclass resolves the table name according to the current search_path. It raises an exception if the name is not valid. See:

  • ERROR: could not find array type for datatype information_schema.sql_identifier

Using information_schema.columns:

SELECT column_name, data_type
FROM   information_schema.columns
WHERE  table_name = 'foo'
ORDER  BY ordinal_position;

The information is standardized, but incomplete:

column_name | data_type
------------+----------------------------
a           | numeric
b           | timestamp without time zone

To get full information for the data type you need to consider all of these columns additionally:

character_maximum_length
character_octet_length
numeric_precision
numeric_precision_radix
numeric_scale
datetime_precision
interval_type
interval_precision

Related answers:

  • How to check if a table exists in a given schema
  • List all columns for a specified table

List of pros & cons

The biggest pros (IMO) in bold.

Information schema views

  • often simpler (depends)
  • slow
  • preprocessed, which may or may not suit your needs
  • selective (users only see objects they have privileges for)
  • conforming to an SQL standard (that's implemented by some of the major RDBMS)
  • mostly portable across major Postgres versions
  • do not require much specific knowledge about Postgres
  • identifiers are descriptive, long and sometimes awkward

System catalogs

  • often more complex (depends), closer to the source
  • fast
  • complete (system columns like oid included)
  • not complying to an SQL standard
  • less portable across major Postgres versions (but basics aren't going to change)
  • require more specific knowledge about Postgres
  • identifiers are terse, less descriptive but conveniently short

Arbitrary query

To get the same list of column names and types from a query, you could use a simple trick: CREATE a temporary table from the query output, then use the same techniques as above.

You can append LIMIT 0, since you do not need actual data:

CREATE TEMP TABLE tmp123 AS
SELECT 1::numeric, now()
LIMIT  0;

To get the data type of individual columns, you can also use the function pg_typeof():

SELECT pg_typeof(1);
2 of 5
4

You can use the psql command line client.

\dt will show a list of tables

\dv will show a list of views

\d [object_name] will describe the schema of the table or view

Not sure how you would describe a query though.

More info: https://manikandanmv.wordpress.com/tag/basic-psql-commands/

🌐
CommandPrompt Inc.
commandprompt.com › education › postgresql-list-all-columns-of-a-specific-table
PostgreSQL - List All Columns of a Specific Table — CommandPrompt Inc.
February 1, 2023 - Alternatively, you can use the “information_schema” with the help of the “SELECT” statement to get the column names of a specific table: SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = 'public' AND ...
Address   2950 Newmarket ST STE 101 - 231, 98226, Bellingham
🌐
GitHub
github.com › brianc › node-postgres › issues › 908
How do I get column names of a table? · Issue #908 · brianc/node-postgres
December 29, 2015 - Hi, I want to get the column names of a table. This is what I'm doing now: var selectQuery = 'SELECT * FROM "' + tableName + '" LIMIT 1'; pgClient.query(selectQuery, function(err, result){ var columns = _.pluck(result.fields, 'name'); })
Author   brianc
🌐
Naysan
naysan.ca › 2022 › 01 › 19 › list-column-names-from-postgresql-table
List Column Names from PostgreSQL Table | Naysan Saran
# If you want to list only the column names select column_name from information_schema.columns where table_name='your_table_name'; # If you also want to add the data type select column_name, data_type from information_schema.columns where table_name='your_table_name';