

We will run the below-given query to alter the data type of both columns in a single statement: ALTER TABLE bike_detailsĪLTER COLUMN bike_model SET DATA TYPE VARCHAR,ĪLTER COLUMN bike_number SET DATA TYPE TEXT Suppose we have to alter the data type of the bike_model column from text to varchar and bike_number column from varchar to text.
Change data type in postgresql how to#
Following will be the syntax for multiple ALTER COLUMN commands: ALTER TABLE tab_nameĪLTER COLUMN col_name_1 TYPE modified_data_type,ĪLTER COLUMN col_name_2 TYPE modified_data_type,ĪLTER COLUMN col_name_N TYPE modified_data_type Įxample #1: How to Alter the Data Type of Multiple Columns? You have to use several ALTER COLUMN commands to alter the data type of multiple columns. How to Alter Multiple Column Type in PostgreSQL? The output shows that the column type has been altered successfully. Let’s run the SELECT command to see the updated table: SELECT * from bike_details The output shows that the bike_details table has been altered successfully. To do so, we will execute the below-given statement: ALTER TABLE bike_details Suppose we have to alter the type of bike_number column from text to varchar.

Let’s run the SELECT query to fetch its details: SELECT * FROM bike_details We have created a bike_details table in our database. Next, specify either the “SET DATA TYPE” or “TYPE” keyword followed by the modified data type.Įxample: How to Alter the Column Type From TEXT to VARCHAR in PostgreSQL? Specify the column name to be altered after the ALTER COLUMN command.

Specify the table name to be altered after the ALTER TABLE command. Here is a simple syntax for altering a single column type in PostgreSQL: ALTER TABLE tab_nameĪLTER COLUMN col_name TYPE modified_data_type This write-up will present a comprehensive guide on how to alter the column type in PostgreSQL. Multiple ALTER COLUMN commands will be used along with the ALTER TABLE command to alter the type of multiple columns in a single statement. The “SET DATA TYPE'' or “TYPE” keyword is used with the collaboration of ALTER TABLE and ALTER COLUMN commands to alter/change the column type in PostgreSQL.
