Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
PostgreSQL - DROP DATABASE
Next article icon

PostgreSQL - DROP DATABASE

Last Updated : 28 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When managing databases in PostgreSQL, you may need to delete an existing database that is no longer required. The DROP DATABASE statement is designed for this purpose. It permanently removes the specified database, along with all of its catalog entries and data directories. This action is irreversible, so it must be executed with caution. Only the database owner has the authority to perform this operation, and all connections to the database must be terminated before proceeding.

Syntax

DROP DATABASE [IF EXISTS] name;

The below rules need to be followed while deleting a database:

  • 'database_name': The name of the database you wish to delete.
  • 'IF EXISTS': This optional clause prevents an error from being thrown if the database does not exist. Instead, PostgreSQL will issue a notice, making your operations smoother.

PostgreSQL DROP DATABASE Example

Below for the sake of example, we will be looking into our system and deleting a few databases, not in use.

Step 1: Listing Available Databases

Firstly we check for the available databases in our system using the below command:

\l

This will list our available database as below:

Listing Available Databases

Here we will be deleting the highlighted databases namely:

  • "my_renamed_db"
  • "my_test_db1"
  • "my_test_db2"
  • "new_test_db"

We will delete these databases as they are no longer in use.

Step 2: Deleting a Single Database

To delete the database 'my_renamed_db', you would use the following command:

DROP DATABASE my_renamed_db;

Now if we check for the available database we will notice that the "my_renamed_db" will be missing from the list as shown in the image below:

Deleting a Single Database

Step 3: Deleting Multiple Databases

Now we will be deleting two databases namely "my_test_db1" and "my_test_db2" using the below commands:

DROP DATABASE my_test_db1;
DROP DATABASE my_test_db2;

After running these commands, both 'my_test_db1' and 'my_test_db2' will be deleted from your system.

Step 4: Deleting the Final Database

Now we will finally delete the last unused database using the below command:

DROP DATABASE new_test_db;

As we check our list of databases we have managed to delete all four of them as intended.

Deleting the Final Database

Using the dropdb Utility Program

In addition to the DROP DATABASE statement, PostgreSQL offers a command-line utility called dropdb. This utility program is a convenient way to remove a database directly from the command line, executing the DROP DATABASE statement behind the scenes.

Syntax for dropdb

The basic syntax to remove a database using dropdb is:

dropdb [options] database_name
  • 'database_name': The name of the database you want to remove.

Example of dropdb

For example, to delete the 'my_renamed_db' database, you would run:

dropdb my_renamed_db

This command will have the same effect as the DROP DATABASE statement, permanently removing the specified database.

Important Rules for Deleting a Database

  • Database Name Specification: Ensure that the exact name of the database you want to delete is specified after the DROP DATABASE clause.
  • Using IF EXISTS: Always use the IF EXISTS clause if there's a chance the database might not exist. This prevents unnecessary errors in your SQL scripts and allows for better error handling.





Next Article
PostgreSQL - DROP DATABASE

R

RajuKumar19
Improve
Article Tags :
  • Python
  • PostgreSQL
  • postgreSQL-managing-database
Practice Tags :
  • python

Similar Reads

    PostgreSQL - Copy Database
    Copying a PostgreSQL database is essential for tasks like testing, backup, and database migration. PostgreSQL offers efficient tools for duplicating databases either within the same server or across different servers, ensuring data consistency and integrity.This guide explains step-by-step methods f
    4 min read
    PostgreSQL ALTER DATABASE
    The PostgreSQL ALTER DATABASE statement is a powerful tool used for modifying an existing database.  The features of a database, once created can be changed using the ALTER DATABASE statement. Let's explore the syntax of PostgreSQL ALTER DATABASE, the various actions that can be performed, and pract
    3 min read
    PostgreSQL - Rename Database
    Renaming a PostgreSQL database is a simple yet essential task for developers and database administrators. Whether we're migrating data, restructuring our database, or just managing our server efficiently, the ability to rename a PostgreSQL database can streamline our workflow. In this article, we wi
    4 min read
    PostgreSQL - Create Database
    Creating a database in PostgreSQL is an important task for developers and database administrators to manage data effectively. PostgreSQL provides multiple ways to create a database, catering to different user preferences, whether through the command-line interface or using a graphical interface like
    5 min read
    PostgreSQL - Restore Database
    Restoring a PostgreSQL database is an essential process for data recovery, especially when dealing with data loss, corruption, or unintentional errors. The ability to efficiently restore a database ensures that an organization can maintain data integrity, availability, and consistency. In this artic
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences