How to add a new column in PostgreSQL?

by hal.littel , in category: PHP Databases , 9 months ago

How to add a new column in PostgreSQL?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by giovanny.lueilwitz , 3 months ago

@hal.littel 

To add a new column to a table in PostgreSQL, you can use the ALTER TABLE statement and the ADD COLUMN clause. Here's the basic syntax:

1
2
ALTER TABLE table_name
ADD COLUMN column_name data_type;


For example, to add a new column called email to a table called customers, you can use the following statement:

1
2
ALTER TABLE customers
ADD COLUMN email VARCHAR(255);


This will add a new column called email to the customers table, with a data type of VARCHAR(255). You can replace VARCHAR(255) with any other data type that is supported by PostgreSQL.


You can also specify additional options when adding a new column, such as a default value or a constraint. For example, to add a new column called age to a table called employees, with a default value of 18, you can use the following statement:

1
2
ALTER TABLE employees
ADD COLUMN age INT DEFAULT 18;


This will add a new column called age to the employees table, with a data type of INT and a default value of 18.


I hope this helps! Let me know if you have any questions.