Skip to content

Instantly share code, notes, and snippets.

@treble37
Forked from miguelmota/rename_column.sql
Created February 8, 2019 22:26
Show Gist options
  • Save treble37/dc03e31206b000f95f47efa760d825ee to your computer and use it in GitHub Desktop.
Save treble37/dc03e31206b000f95f47efa760d825ee to your computer and use it in GitHub Desktop.
PostgreSQL rename column if not exists
DO $$
BEGIN
IF NOT EXISTS(SELECT *
FROM information_schema.columns
WHERE table_name='my_table' and column_name='my_column')
THEN
ALTER TABLE "public"."my_table" RENAME COLUMN "my_column" TO "my_new_column";
END IF;
END $$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment