PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • Aggregate Functions
    • Date / Time Functions
    • String Functions
    • Math Functions
Home / PostgreSQL Tutorial / PostgreSQL Change Column Type: Step-by-Step Examples

PostgreSQL Change Column Type: Step-by-Step Examples

Summary: this tutorial shows you step by step how to change the data type of a column by using the ALTER TABLE statement.

PostgreSQL change column type statement

To change the data type of a column, you use the ALTER TABLE statement as follows:

1
2
3
ALTER TABLE table_name
ALTER COLUMN column_name [SET DATA] TYPE new_data_type;
 

Let’s examine the statement in a greater detail:

  • First, specify the name of the table to which the column you want to change belong in the ALTER TABLE clause.
  • Second, give the name of column whose data type will be changed in the ALTER COLUMN clause.
  • Third, provide the new data type for the column after the TYPE keyword. It is possible to use either SET DATA TYPE or TYPE.

To change the data types of multiple columns in a single statement, you use the following syntax:

1
2
3
4
ALTER TABLE table_name
ALTER COLUMN column_name_1 [SET DATA] TYPE new_data_type,
ALTER COLUMN column_name_2 [SET DATA] TYPE new_data_type,
...;

In this syntax, you separate each ALTER COLUMN clause by a comma (,) in order to change the types of multiple columns at a time.

PostgreSQL allows you to convert old column values to the new one while changing the data type of a column by adding a USING clause as follows:

1
2
ALTER TABLE table_name
ALTER COLUMN column_name TYPE new_data_type USING expression;

The USING clause allows you to compute the new column values from the old ones.

If you omit the USING clause, PostgreSQL will cast old column values to the new one implicitly. In case the casting is failed, PostgreSQL will issue an error and ask you to provide the USING clause with an expression for conversion.

An expression could be as simple as column_name::new_data_type such as price::numeric or as complex as a custom function.

PostgreSQL change column type examples

Let’s create a new table named assets and insert some rows into the table for the demonstration.

PostgreSQL Change Column Type Sample Table

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
CREATE TABLE assets (
    id serial PRIMARY KEY,
    name TEXT NOT NULL,
    asset_no VARCHAR NOT NULL,
    description TEXT,
    LOCATION TEXT,
    acquired_date DATE NOT NULL
);
 
INSERT INTO assets (
    NAME,
    asset_no,
    location,
    acquired_date
)
VALUES
    (
        'Server',
        '10001',
        'Server room',
        '2017-01-01'
    ),
    (
        'UPS',
        '10002',
        'Server room',
        '2

To change the data type of the name column to VARCHAR, you use the following statement:

1
ALTER TABLE assets ALTER COLUMN name TYPE VARCHAR;

The following statement changes the data types of description and location columns from TEXT to VARCHAR:

1
2
3
ALTER TABLE assets
    ALTER COLUMN location TYPE VARCHAR,
    ALTER COLUMN description TYPE VARCHAR;

To change the data type of the asset_no column to integer, you use the statement below:

1
ALTER TABLE assets ALTER COLUMN asset_no TYPE INT;

PostgreSQL issued an error and a very useful hint to ask you to specify the USING clause:

1
2
ERROR:  column "asset_no" cannot be cast automatically to type integer
HINT:  You might need to specify "USING asset_no::integer".

Let’s add the USING clause as recommended:

1
2
ALTER TABLE assets
    ALTER COLUMN asset_no TYPE INT USING asset_no::integer;

It worked.

In this tutorial, you have learned how to change the type of a column using the ALTER TABLE statement.

Previous Tutorial: PostgreSQL DROP COLUMN: Remove One or More Columns of a Table
Next Tutorial: PostgreSQL RENAME COLUMN: Renaming One or More Columns of a Table

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete

Managing Table Structure

  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

PostgreSQL Views

  • Managing PostgreSQL Views
  • Creating Updatable Views
  • PostgreSQL Materialized Views
  • The WITH CHECK OPTION Views
  • PostgreSQL Recursive View

PostgreSQL Triggers

  • Introduction to Trigger
  • Creating A Trigger
  • Managing PostgreSQL Triggers

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL TRUNC
  • PostgreSQL ROUND
  • PostgreSQL Math Functions
  • PostgreSQL LOCALTIME
  • PostgreSQL LOCALTIMESTAMP
  • PostgreSQL CURRENT_TIMESTAMP
  • PostgreSQL CURRENT_TIME
  • PostgreSQL CURRENT_DATE
  • PostgreSQL Date Functions
  • PostgreSQL EXTRACT

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.