PostgreSQL – Add Column
To add a new column to an existing PostgreSQL Table, use the following ALTER TABLE syntax.
</>
Copy
ALTER TABLE table_name
ADD COLUMN new_column_name data_type;
Example – Add Column to PostgreSQL Table
Consider the following table, where we have three columns.
Now we will add a new column named attendance
to this table.
</>
Copy
ALTER TABLE students
ADD COLUMN attendance INTEGER;
A new column with the name attendance
and datatype of integer
has been added to the Table. But the value for this column for each row has been null
.
Example – Add Column with a Default Value
To add a new column with some default value other than null, use the following ALTER TABLE syntax.
</>
Copy
ALTER TABLE table_name
ADD COLUMN new_column_name data_type NOT NULL DEFAULT default_value;
Let us create a column named attendance with default value of 84.
The contents of the table after executing the ALTER TABLE … ADD COLUMN … would be
Conclusion
In this PostgreSQL Tutorial, we have added a new column to PostgreSQL Table. Also, we have seen how to add a column with a default value.