PostgreSQL Alter table add multiple columns to existing table

To add multiple columns to an existing table in PostgreSQL, you can use the ALTER TABLE statement with the ADD COLUMN clause. Here’s the syntax for adding multiple columns at once:

Syntax

ALTER TABLE table_name
ADD COLUMN column1_name data_type1,
ADD COLUMN column2_name data_type2,
...
ADD COLUMN columnN_name data_typeN;

Let’s break down the syntax:

ALTER TABLE table_name: This specifies the name of the table to which you want to add columns.
ADD COLUMN column_name data_type: This clause is used to add a new column to the table. You need to provide the name of the column and its data type.
You can repeat the ADD COLUMN clause for each column you want to add, separating them with commas. Make sure to specify the column name and its corresponding data type.

Example

Here’s an example that demonstrates adding multiple columns to an existing table called employees:

ALTER TABLE employees
ADD COLUMN email VARCHAR(255),
ADD COLUMN phone_number VARCHAR(20),
ADD COLUMN hire_date DATE;

In the above example, we add three columns to the employees table. The first column is email of type VARCHAR(255), the second column is phone_number of type VARCHAR(20), and the third column is hire_date of type DATE.

Remember to adjust the column names and data types according to your specific requirements.

Note that when adding columns to an existing table, you need to be mindful of the data already present in the table. If the table already contains data, you may need to provide default values for the newly added columns or update the existing rows accordingly.