HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

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

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
generatedwithoutpostgrescolumnconvertnonplaceregulardrop

Problem

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?

Solution

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

Code Snippets

ALTER TABLE test_table ALTER COLUMN the_column_to_alter DROP EXPRESSION;

Context

StackExchange Database Administrators Q#294850, answer score: 7

Revisions (0)

No revisions yet.