ALTER TABLE ... ALTER COLUMN ... TYPE converts an existing column to a new data type, optionally with a USING expression for safe casting.
Use ALTER TABLE ... ALTER COLUMN ... TYPE to convert a column to a different data type without recreating the table.
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.
Run ALTER TABLE <table> ALTER COLUMN <column> TYPE <new_type> [USING expression] [CASCADE]; see full syntax below.
Provide a USING clause to transform existing values. For example, USING price::numeric(12,2) converts text price values safely.
Yes, but the column is rewritten, and all referencing foreign keys are checked. Use CASCADE to update dependent objects automatically.
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.
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.
PostgreSQL drops and recreates indexes on the altered column automatically. Check index size afterwards to ensure optimal performance.
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.
Yes, it takes an ACCESS EXCLUSIVE lock, blocking writes. Reads continue unless a physical rewrite is required.
Yes, chain multiple ALTER COLUMN statements separated by commas inside a single ALTER TABLE command.
Yes. The default sequence and nextval() remain attached after changing from INT to BIGINT.