postgresql - 获取两个表之间的列差异列表

postgresql - get a list of columns difference between 2 tables

我们使用的是使用 Postgres 8 的 Redshift。
我需要比较 (2) table 几乎相同,但另一个 table 会有额外的列,所以我需要找出列差异。

示例:

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,
)

我可以使用什么查询来获得列差异列表?

您可以通过选择 table2not 也出现在 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'
    )