
old_col_name is a column to be altered. RENAME and TO are predefined clauses used to rename a column. Here, ALTER TABLE is a command used to alter a table in PostgreSQL. The output verified that the “team_details” table had been renamed to the “team_info”.Ī table column in PostgreSQL can be renamed/modified using the "ALTER TABLE" command: ALTER TABLE tbl_name Let’s run the “\dt” command to get the list of relations/tables: \dt “team_info” represents the modified table name. “team_details” is a table name to be altered/renamed. Here, we utilized the “RENAME TO” along with the “ALTER TABLE” command to rename a table. Let’s execute the “ALTER TABLE” command to rename the “team_details” table to “team_info”: ALTER TABLE team_details RENAME TO team_info
#ALTER TABLE POSTGRESQL HOW TO#
tbl-name represents the table's name, while modified_tbl_name represents the altered/modified table name.Įxample: How to Rename a Table using the ALTER TABLE Clause? RENAME and TO are predefined clauses in PostgreSQL that can be used to rename a table. The “ALTER TABLE” command can be used along with the “RENAME TO” clause to rename a specific table: ALTER TABLE tbl_name RENAME TO modified_tbl_name The output verified that the “team_targets” column had been dropped from the “team_details” table using the “ALTER TABLE”command. Let’s verify the working of the “ALTER TABLE” command by executing the following command: \d team_details On successful execution of the “ALTER TABLE” command, a column named “team_targets” will be dropped from the “team_details” table: Run the following command to drop/delete a column from any specific table: ALTER TABLE team_details DROP team_targets col_name represents the column to be dropped.Įxample: How to Use the ALTER TABLE Command to Drop a Column? DROP is a reserved keyword to drop/delete a specific column from the table. Here, the ALTER TABLE command/statement is used to drop a column from a table. You have to follow the below-given syntax to drop a column using the ALTER TABLE command in PostgreSQL: ALTER TABLE tbl_name DROP col_name The above snippet verifies the working of the “ALTER TABLE” command. Let’s execute the “\d” command to verify that the targeted table has been altered successfully: \d team_details Step 4: Describe the Altered/Modified Table So, on successful execution of the “ALTER TABLE” command, a new column named “team_ranking” of “integer” data type will be added to the table named “team_details”. team_ranking is the column name, while int is the data type of that column. To do so, you can run the “ALTER TABLE” command as follows: ALTER TABLE team_details ADD team_ranking int Let’s assume you have to add a new column in the “team_details” table. Here, “\d” is the command while “team_details” is the table to be described: Once you are connected to the targeted database, run the “\d” command to get all the necessary details about that table: \d team_details

Open the psql, and run the “\c” command to connect/access the desired table as shown below: \c example col_name is a column name to be added to the targeted table, while data_type represents the column’s type.Įxample: How to add a column with the ALTER TABLE command/statement?įollow the below-listed guidelines to learn how to add a new column in a table using ALTER TABLE: ADD is a reserved keyword used to add/insert a column in a table. The tbl-name is the name of the targeted table. Here, the ALTER TABLE command/statement is used to add a column in a table. To do so, follow the below-given syntax: ALTER TABLE tbl_name ADD col_name data_type

In PostgreSQL, the ALTER TABLE command/clause can be used to add columns to any specific table.

How to Add/Insert a Column in PostgreSQL? This write-up will explain the multiple use-cases of the ALTER TABLE command in PostgreSQL. So, all in all, we can say that the ALTER TABLE command is used to change/modify the table structure. Moreover, it allows us to add or remove constraints to a table.
#ALTER TABLE POSTGRESQL UPDATE#
For example, the ALTER TABLE statement can add, drop, or update the table columns. In PostgreSQL, the ALTER TABLE command performs different functionalities on a table.
