Search⌘ K

Advanced Data Definition Commands to Alter Tables

Explore how to manage PostgreSQL databases by learning advanced data definition commands such as DROP and ALTER. Understand how to delete or rename databases and tables safely, including using the IF EXISTS clause to avoid errors and ensure smooth database operations.

Deleting a database

To delete a database, we can use the DROP DATABASE command. This will permanently delete the database and all of its contents.

The syntax for this command is given below:

PostgreSQL
DROP DATABASE <database_name>;

For example, to delete a database named customer_db, the command would be as follows:

PostgreSQL
\l customer_db
SELECT '\n' AS " "; -- Adding new line
DROP DATABASE customer_db; -- DROP DATABASE command
\l customer_db
  • Lines 1 and 6: The \l <database_name> command is used to list the properties of the specified database schema. This is to verify that the database has been deleted successfully.

  • Line 2: This statement returns a new line as a column called " " in the results. The \n character is a newline character used to create a new line in the output. The -- indicates that the remainder of the line is a comment that the database ignores.

  • Line 4: The DROP DATABASE command will delete the database with the name customer_db.

Note: Before deleting a database, make ...