PostgreSQL – Add Column

To add a new column to an existing PostgreSQL Table, use the following ALTER TABLE syntax.

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.

ADVERTISEMENT
PostgreSQL INSERT INTO table

Now we will add a new column named attendance to this table.

ALTER TABLE students
	ADD COLUMN attendance INTEGER;
PostgreSQL Add new Column to Table

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.

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.

PostgreSQL Add Column with Default Value

The contents of the table after executing the ALTER TABLE … ADD COLUMN … would be

PostgreSQL Add Column with Default Value

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.