Convert a postgres GENERATED column to a regular non-generated column (in place without DROP)

All we need is an easy explanation of the problem, so here it is.

I’m using postgres 13.3, and I already have an existing table with a GENERATED column such as:

CREATE TABLE test_table (
    id uuid NOT NULL DEFAULT uuid_generate_v4(),
    the_column_to_alter bool NOT NULL GENERATED ALWAYS AS (2 > 1) STORED,
    CONSTRAINT test_table2_pkey PRIMARY KEY (id)
);

But I’ve now changed my mind, and I want this to instead just be a regular non-generated column that I can edit manually…

  • But I want to avoid having to DROP and re-create the column
  • And I want to retain the existing values that are already in the column at the moment

Is this possible? What would the ALTER query be to do this for the the_column_to_alter column?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Here’s the command for the example table in the original post:

ALTER TABLE test_table ALTER COLUMN the_column_to_alter DROP EXPRESSION;

The postgres manual page is here: https://www.postgresql.org/docs/current/sql-altertable.html

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply