81

Is there a simple alternative in PostgreSQL to this statement produced in Oracle?

select table_name from user_tab_columns
where table_name = myTable and column_name = myColumn;

I am then testing whether the query returns anything so as to prove the column exists.

I am aware that using psql I can find these out individually but this is required to produce a result in a program I am writing to validate that a requested attribute field exists in my database table.

6 Answers 6

151

Try this :

SELECT column_name 
FROM information_schema.columns 
WHERE table_name='your_table' and column_name='your_column';
3
  • Thanks. Accepted as answer as it is a direct alternative to what I was doing in Oracle.
    – CSharpened
    Apr 3, 2012 at 10:13
  • @CSharpened what is the query for Oracle?
    – Evgeny
    Apr 18, 2013 at 19:41
  • 1
    This is an extremely fragile answer - e.g,. if a table called your_table appears in a schema that is higher up in search_path. My PostGIS database has monthly schema, each with identical table names; using this answer, vicmap201208.address would be found before vicmap201910.address because vicmap201208 appears before vicmap201910 on search_path (for good reasons that I wouldn't want to change just to make a bad answer work). [As it happens, the column names are constant across schemas: that's not the point]
    – GT.
    Dec 3, 2019 at 5:27
57

Accepted answer is correct, but is missing the schema and nicer output (True/False):

SELECT EXISTS (SELECT 1 
FROM information_schema.columns 
WHERE table_schema='my_schema' AND table_name='my_table' AND column_name='my_column');
2
  • Also takes in account the schema Sep 18, 2018 at 9:56
  • 2
    Or just SELECT true as exists FROM information_schema.columns WHERE ...;
    – yoyo
    Apr 16, 2021 at 22:25
26

Simpler and SQLi-safe using PostgreSQL's object identifier types:

SELECT true
FROM   pg_attribute 
WHERE  attrelid = 'myTable'::regclass  -- cast to a registered class (table)
AND    attname = 'myColumn'
AND    NOT attisdropped  -- exclude dropped (dead) columns
-- AND attnum > 0        -- exclude system columns (you may or may not want this)

System catalogs are many times faster than querying the notoriously convoluted information_schema (but still just milliseconds for a single query). See:

Read about the significance of the columns in the manual.

While building dynamic SQL with the column name supplied as parameter, use quote_ident() to defend against SQL injection:

...
AND    attname = quote_ident('myColumn');

Works for tables outside the search_path, too:

...
WHERE  attrelid = 'mySchema.myTable'::regclass
...
5

Unlike Oracle, PostgreSQL supports the ANSI standard INFORMATION_SCHEMA views.

The corresponding standard view to Oracle's user_tab_columns is information_schema.columns

http://www.postgresql.org/docs/current/static/infoschema-columns.html

4
SELECT attname 
FROM pg_attribute 
WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'YOURTABLENAME') 
AND attname = 'YOURCOLUMNNAME';

Of course, replace YOURTABLENAME and YOURCOLUMNNAME with the proper values. If a row is returned, a column with that name exists, otherwise it does not.

1
  • Thanks for the response. Although your example works fine I have opted to accept Ramandeeps answer due to its simplicity and the fact that it seems a more direct alternative to my issue.
    – CSharpened
    Apr 3, 2012 at 10:14
2

Here is a similar variant of Erwin Brandstetter answer. Here we check schema too in case we have similar tables in different schema.

SELECT TRUE FROM pg_attribute 
WHERE attrelid = (
    SELECT c.oid
    FROM pg_class c
    JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE 
        n.nspname = CURRENT_SCHEMA() 
        AND c.relname = 'YOURTABLENAME'
    )
AND attname = 'YOURCOLUMNNAME'
AND NOT attisdropped
AND attnum > 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.