Changing the data type of a column

Thankfully, changing column data types is not an everyday task, but when we need to do it, we must know all the details so that we can perform the conversion on a production system without any errors.

Getting ready

Let's start with a simple example of a table, as follows:

postgres=# select * from birthday;

This gives the following output:

 name  |  dob   
-------+--------
 simon | 690926

(1 row)

The preceding table was created using this command:

CREATE TABLE birthday
( name       TEXT
, dob        INTEGER);

How to do it…

Let's say we want to change the dob column to another data type. Let's try with a simple example first, as follows:

postgres=# ALTER TABLE birthday
postgres-# ALTER COLUMN dob SET DATA TYPE text;
ALTER TABLE

Get PostgreSQL 9 Administration Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.