To change a column’s data type in PostgreSQL, use ALTER TABLE … ALTER COLUMN … TYPE. PostgreSQL converts every value already in the column, and when it can’t do that by itself, USING tells it how. Here’s how to do both, with a small example.
Every command and output on this page is real, from PostgreSQL 19 on macOS.
Syntax
ALTER TABLE table_name
ALTER COLUMN column_name TYPE new_type [ USING expression ];
table_nameandcolumn_name: the table, and the column to change.new_type: the new data type, such asintegerorvarchar(20).SET DATA TYPEis a longer way to writeTYPE.USING expression: optional. How to work out each new value from the old one, such ascolumn_name::integer. It’s needed when PostgreSQL has no automatic conversion between the two types.[ ]marks the optional part.
See ALTER TABLE (opens in a new tab) in the PostgreSQL documentation.
The Example
A quiz keeps its scores in a table, but the points were saved as text, and player names may have at most 5 characters. You change the points to whole numbers and allow longer names.
Step 1: Create a Test Database
Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.
Create a database for the example and connect to it:
CREATE DATABASE quiz;
CREATE DATABASE
\c quiz
You are now connected to database "quiz" as user "postgres".
Step 2: Create the Table and Add Two Scores
CREATE TABLE scores (player varchar(5), points text);
CREATE TABLE
INSERT INTO scores (player, points) VALUES ('Ana', '12');
INSERT 0 1
INSERT INTO scores (player, points) VALUES ('Ben', '7');
INSERT 0 1
Step 3: Change Text to a Number
ALTER TABLE scores ALTER COLUMN points TYPE integer;
ERROR: column "points" cannot be cast automatically to type integer
HINT: You might need to specify "USING points::integer".
PostgreSQL won’t turn text into a number by itself, and its hint says what to add: USING.
ALTER TABLE scores ALTER COLUMN points TYPE integer USING points::integer;
ALTER TABLE
points::integer converts each value to a whole number, and the column’s type changes with it.
Step 4: Make a Text Column Longer
ALTER TABLE scores ALTER COLUMN player TYPE varchar(20);
ALTER TABLE
No USING this time: PostgreSQL converts from one text type to another by itself.
Step 5: See the New Types
\d scores
Table "public.scores"
Column | Type | Collation | Nullable | Default
--------+-----------------------+-----------+----------+---------
player | character varying(20) | | |
points | integer | | |
character varying(20) is the full name of varchar(20). The values haven’t changed:
SELECT * FROM scores;
player | points
--------+--------
Ana | 12
Ben | 7
(2 rows)
psql now lines the points up on the right, as it does with numbers.
Clean Up
To remove the example, connect to another database and drop the test database:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE quiz;
DROP DATABASE
Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out USING where it’s needed | ERROR: column "points" cannot be cast automatically to type integer | Add USING points::integer, as the hint says. |
Converting a value that doesn’t fit the new type, such as 'ten' to integer | ERROR: invalid input syntax for type integer: "ten", and PostgreSQL changes nothing. | Fix or remove those values first, then run the change again. |
| Making a text column shorter than its values | ERROR: value too long for type character varying(2) | Choose a length that fits the longest value. |
FAQ
Does changing a type rewrite the table?
Usually, yes: the documentation (opens in a new tab) says the whole table and its indexes are normally rewritten, which takes longer on a big table, and it lists the exceptions.
Conclusion
ALTER TABLE … ALTER COLUMN … TYPE changes a column’s type and converts its values. Add USING when PostgreSQL can’t convert by itself, and fix any value that won’t fit before you change the type.
Additional Resources
- PostgreSQL 19 documentation: ALTER TABLE (opens in a new tab)
- PostgreSQL 19 documentation: Changing a Column’s Data Type (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 6: Create Tables
- How to Add a Column to a Table in PostgreSQL
- How to Rename a Column in PostgreSQL
Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.
0 comments