Site icon JD Bots

How to Change a Column’s Data Type in PostgreSQL

Featured card for How to Change a Column’s Data Type in PostgreSQL, with a terminal showing ALTER COLUMN points TYPE integer USING points::integer, and varchar(5) becoming varchar(20).

How to Change a Column’s Data Type in PostgreSQL

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

Syntax

ALTER TABLE table_name
    ALTER COLUMN column_name TYPE new_type [ USING expression ];

See ALTER TABLE 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:

postgres=#

CREATE DATABASE quiz;

Output

CREATE DATABASE

postgres=#

\c quiz

Output

You are now connected to database "quiz" as user "postgres".

Step 2: Create the Table and Add Two Scores

quiz=#

CREATE TABLE scores (player varchar(5), points text);

Output

CREATE TABLE

quiz=#

INSERT INTO scores (player, points) VALUES ('Ana', '12');

Output

INSERT 0 1

quiz=#

INSERT INTO scores (player, points) VALUES ('Ben', '7');

Output

INSERT 0 1

Step 3: Change Text to a Number

quiz=#

ALTER TABLE scores ALTER COLUMN points TYPE integer;

Output

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.

quiz=#

ALTER TABLE scores ALTER COLUMN points TYPE integer USING points::integer;

Output

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

quiz=#

ALTER TABLE scores ALTER COLUMN player TYPE varchar(20);

Output

ALTER TABLE

No USING this time: PostgreSQL converts from one text type to another by itself.

Step 5: See the New Types

quiz=#

\d scores

Output

                      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:

quiz=#

SELECT * FROM scores;

Output

 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:

quiz=#

\c postgres

Output

You are now connected to database "postgres" as user "postgres".

postgres=#

DROP DATABASE quiz;

Output

DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
Leaving out USING where it’s neededERROR: column "points" cannot be cast automatically to type integerAdd USING points::integer, as the hint says.
Converting a value that doesn’t fit the new type, such as 'ten' to integerERROR: 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 valuesERROR: 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 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

Related

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

Exit mobile version