How to Change Column Type in PostgreSQL

Galaxy Glossary

How do I change a column’s data type in PostgreSQL without losing data?

ALTER TABLE ... ALTER COLUMN ... TYPE converts an existing column to a new data type, optionally with a USING expression for safe casting.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

How to Change Column Type in PostgreSQL

Use ALTER TABLE ... ALTER COLUMN ... TYPE to convert a column to a different data type without recreating the table.

Why would you change a column type?

Schema evolution, precision needs, or performance tuning often require switching from smaller to larger numeric types, from text to JSONB, or from VARCHAR to TEXT. ALTER COLUMN TYPE handles these migrations in-place.

What is the exact command?

Run ALTER TABLE <table> ALTER COLUMN <column> TYPE <new_type> [USING expression] [CASCADE]; see full syntax below.

How do I handle incompatible data?

Provide a USING clause to transform existing values. For example, USING price::numeric(12,2) converts text price values safely.

Can I change a primary-key column?

Yes, but the column is rewritten, and all referencing foreign keys are checked. Use CASCADE to update dependent objects automatically.

Example: convert integer to bigint

ALTER TABLE sales
ALTER COLUMN order_id TYPE bigint;

The table rewrites only if a physical storage change is required. PostgreSQL locks the table for writes during the rewrite.

Example: text to JSONB with USING

ALTER TABLE events
ALTER COLUMN payload TYPE jsonb
USING payload::jsonb;

Always test the USING expression on a subset of rows to confirm it parses correctly.

Best practices for ALTER COLUMN TYPE

  • Create a backup before large rewrites.
  • Run in maintenance windows for big tables.
  • Use USING to avoid runtime cast errors.
  • Add indexes after the type change to prevent long rebuilds.

What happens to indexes and constraints?

PostgreSQL drops and recreates indexes on the altered column automatically. Check index size afterwards to ensure optimal performance.

How to revert if something fails?

Use a transactional block: BEGIN; ALTER TABLE ...; ROLLBACK; test inside a transaction on staging. If committed already, restore from backup or run another ALTER to the old type.

Why How to Change Column Type in PostgreSQL is important

How to Change Column Type in PostgreSQL Example Usage


ALTER TABLE public.orders
ALTER COLUMN amount TYPE numeric(12,2)
USING amount::numeric(12,2);

How to Change Column Type in PostgreSQL Syntax


ALTER TABLE [IF EXISTS] table_name
    ALTER [COLUMN] column_name SET DATA TYPE new_type
        [COLLATE collation]
        [USING expression]
        [CASCADE | RESTRICT];

Common Mistakes

Frequently Asked Questions (FAQs)

Does ALTER COLUMN TYPE lock the table?

Yes, it takes an ACCESS EXCLUSIVE lock, blocking writes. Reads continue unless a physical rewrite is required.

Can I change multiple columns at once?

Yes, chain multiple ALTER COLUMN statements separated by commas inside a single ALTER TABLE command.

Will sequences tied to SERIAL columns still work?

Yes. The default sequence and nextval() remain attached after changing from INT to BIGINT.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo