问题
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?
回答1:
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'
)
来源:https://stackoverflow.com/questions/36733941/postgresql-get-a-list-of-columns-difference-between-2-tables