1

We are using Redshift which is using Postgres 8.
I need to compare (2) tables which will almost be identical, but the other table will have extra columns so, I need to find out the column difference.

Example:

CREATE TABLE table1 (
    v_id character varying(255) NOT NULL,
    v_created timestamp without time zone NOT NULL,
    abc_102 boolean,
    abc_103 boolean,
    abc_104 boolean,
    def_56 boolean DEFAULT false NOT NULL,
    def_57 boolean DEFAULT false NOT NULL
)

CREATE TABLE table2 (
    v_id character varying(255) NOT NULL,
    v_created timestamp without time zone NOT NULL,
    abc_102 boolean,
    abc_103 boolean,
    abc_104 boolean,
    abc_105 boolean,
    def_56 boolean DEFAULT false NOT NULL,
    def_57 boolean DEFAULT false NOT NULL,
    def_58 boolean DEFAULT false NOT NULL,
)

What query can I use that will give me a list of the column differences?

noober
  • 1,427
  • 3
  • 23
  • 36

1 Answers1

2

You can achieve this by selecting all column names from table2 which do not also appear in table1:

SELECT column_name
FROM information_schema.columns 
WHERE table_schema = 'your_schema' AND table_name = 'table2'
    AND column_name NOT IN
    (
        SELECT column_name
        FROM information_schema.columns 
        WHERE table_schema = 'your_schema' AND table_name = 'table1'
    )
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360