{"id":1531,"date":"2023-08-05T09:23:54","date_gmt":"2023-08-05T09:23:54","guid":{"rendered":"https:\/\/www.dedicatedcore.com\/blog\/?p=1531"},"modified":"2024-11-26T11:35:03","modified_gmt":"2024-11-26T11:35:03","slug":"drop-postgresql-database","status":"publish","type":"post","link":"https:\/\/www.dedicatedcore.com\/blog\/drop-postgresql-database\/","title":{"rendered":"How to Drop a Database in PostgreSQL"},"content":{"rendered":"<p>A robust open-source relational database management system called PostgreSQL provides a wealth of features for effectively managing and organizing data. Removing a database is one crucial action that needs to be done carefully to avoid accidental data loss. As one is facing performance issues due to <a href=\"https:\/\/www.dedicatedcore.com\/blog\/find-kill-terminate-mysql-process\/\" target=\"_blank\" rel=\"noopener\">problematic long-running queries<\/a> searching and killing MySQL processes is necessary and helpful.<\/p>\n<p>The vps server hosting offers a <a href=\"https:\/\/www.dedicatedcore.com\/vps-hosting-singapore\/\" target=\"_blank\" rel=\"noopener\">virtual machine in SG (Singapore)<\/a> to give every service that your server needs. We will examine the several facets of dropping databases in this investigation of &#8220;PostgreSQL Drop Database,&#8221; offering lucid examples and instructions to guarantee a smooth performance of this crucial function.<\/p>\n<p>A database can be dropped using one of two command-line options provided by PostgreSQL: a shell utility or the DROP DATABASE statement. It&#8217;s a good idea to remove unused databases to maintain a tidy workstation. However, be aware that removing an existing PostgreSQL database also eliminates all of its data and catalog entries. We allow private servers powerful web hardware and <a href=\"https:\/\/www.dedicatedcore.com\/vps-hosting-india\/\" target=\"_blank\" rel=\"noopener\">trouble-free VPS operation<\/a> for India to deliver a pre-eminent hosting experience.<\/p>\n<p><strong>Required Conditions<\/strong><\/p>\n<ul>\n<li>Sudo rights to access the terminal.<\/li>\n<li>Installed and configured PostgreSQL 10 or higher (use our instructions for Windows or Ubuntu; if it&#8217;s already installed, see the PostgreSQL version on the system).<\/li>\n<\/ul>\n<h2>DROP Database<\/h2>\n<p>Using the following SQL statement is the first way to delete a PostgreSQL database:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">DROP DATABASE &lt;database name&gt;;<\/pre>\n<p>The directory holding the catalog entries and database data is deleted by the command. The DROP DATABASE command can only be used by the database owner. The command is not executed if the database is being used by anyone.<\/p>\n<p>To see how DROP DATABASE works, try the following:<\/p>\n<p>1. Press CTRL+ALT+T to open the terminal.<\/p>\n<p>2. Set up a PostgreSQL connection:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo -i -u postgres psql<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1534\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/sudo-id-u-postgresql-command.png\" alt=\"command displays terminal output\" width=\"800\" height=\"209\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/sudo-id-u-postgresql-command.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/sudo-id-u-postgresql-command-300x78.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/sudo-id-u-postgresql-command-150x39.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/sudo-id-u-postgresql-command-768x201.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/sudo-id-u-postgresql-command-100x26.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/sudo-id-u-postgresql-command-700x183.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>3. Construct the example&#8217;s database:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">CREATE DATABASE example;<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1535\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/create-database-example.png\" alt=\"terminal prints statement\" width=\"800\" height=\"169\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/create-database-example.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/create-database-example-300x63.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/create-database-example-150x32.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/create-database-example-768x162.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/create-database-example-100x21.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/create-database-example-700x148.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>The executed statement is printed by the terminal.<\/p>\n<p>4. Enumerate every database using:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\\l<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1536\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/command-list-all-databases.png\" alt=\"database shows the list\" width=\"800\" height=\"391\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/command-list-all-databases.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/command-list-all-databases-300x147.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/command-list-all-databases-150x73.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/command-list-all-databases-768x375.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/command-list-all-databases-100x49.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/command-list-all-databases-700x342.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>The list contains the database from the previous step.<\/p>\n<p>5. Delete the database using<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">DROP DATABASE example;<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1537\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-example.png\" alt=\"drop database command\" width=\"800\" height=\"151\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-example.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-example-300x57.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-example-150x28.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-example-768x145.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-example-100x19.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-example-700x132.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>The executed statement is visible in the output.<\/p>\n<p>6. Relist every database:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\\l<\/pre>\n<p>The list no longer includes the sample database.<\/p>\n<h3>IF EXITS Option<\/h3>\n<p>For all versions where DROP DATABASE is accessible, the IF EXISTS option is active. The following is the whole command syntax when the IF EXISTS option is used:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">DROP DATABASE IF EXISTS &lt;database name&gt;;<\/pre>\n<p>Before deleting a database, the option first verifies that it is there. The command removes the database if one is present. On the other hand, the command prints an instructive notification message in the event that a database is absent.<\/p>\n<p>To see how the command functions, take the following actions:<\/p>\n<p>1. Make a sample database:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">CREATE DATABASE example;<\/pre>\n<p>2. Use the IF EXISTS option to drop the database:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">DROP DATABASE IF EXISTS example;<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1538\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-if-exits-example.png\" alt=\"displays about database does not exist\" width=\"800\" height=\"190\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-if-exits-example.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-if-exits-example-300x71.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-if-exits-example-150x36.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-if-exits-example-768x182.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-if-exits-example-100x24.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-if-exits-example-700x166.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>If the database is real, the outcome is the same as with DROP DATABASE.<\/p>\n<p>3. The database isn&#8217;t accessible anymore. To view the output, rerun the command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">DROP DATABASE IF EXISTS example;<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1539\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database.png\" alt=\"drop database if exist command\" width=\"800\" height=\"170\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-300x64.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-150x32.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-768x163.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-100x21.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-700x149.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>The database does not exist, according to the notice message that prints.<\/p>\n<p>4. Use the following command to illustrate how using IF EXISTS differs from leaving the option out:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">DROP DATABASE example;<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1540\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-example-command.png\" alt=\"drop database without if exists option shows error\" width=\"800\" height=\"149\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-example-command.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-example-command-300x56.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-example-command-150x28.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-example-command-768x143.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-example-command-100x19.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-example-command-700x130.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>An error message appears when you use DROP DATABASE on a non-existent database without using the IF EXISTS option.<\/p>\n<h4>INCLUDING (FORCE)<\/h4>\n<p>PostgreSQL versions 13 and above support the WITH (FORCE) option.<\/p>\n<p>If the database is being used, the DROP DATABASE command will not delete it. The terminal prints an error indicating that a database session is open if the database is in use.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1541\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-error-session.png\" alt=\"error message if database is in use\" width=\"800\" height=\"169\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-error-session.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-error-session-300x63.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-error-session-150x32.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-error-session-768x162.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-error-session-100x21.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-error-session-700x148.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>To forcibly end the session and erase the database, add the WITH (FORCE) option:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">DROP DATABASE &lt;database name&gt; WITH (FORCE);<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1542\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-with-force.png\" alt=\"closes user session and delete database\" width=\"800\" height=\"210\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-with-force.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-with-force-300x79.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-with-force-150x39.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-with-force-768x202.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-with-force-100x26.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-database-with-force-700x184.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>Postgres tries to forcefully erase the database and end the user&#8217;s session.<\/p>\n<h4>Dropdb Utility<\/h4>\n<p>The DROP DATABASE command is wrapped in the dropdb shell program. The two approaches are the same in practice. Dropdb, on the other hand, provides more choices, such as remote database removal.<\/p>\n<p>The fundamental syntax is as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">dropdb &lt;connection parameters&gt; &lt;options&gt; &lt;database name&gt;<\/pre>\n<p>To observe how dropdb functions with the -i and -e arguments, for instance, try running the following command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">dropdb -i -e example<\/pre>\n<p>Because of the -i tag, the application requests confirmation before deleting anything.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1543\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-i-e-example.png\" alt=\"prints command generated to server\" width=\"800\" height=\"229\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-i-e-example.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-i-e-example-300x86.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-i-e-example-150x43.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-i-e-example-768x220.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-i-e-example-100x29.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/drop-i-e-example-700x200.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>To confirm, press Y. The created commands are printed to the server by the application. Because the database is non-existent, the software throws an error and terminates.<\/p>\n<h4>Wrapping Up on How to Drop PostgreSQL Database<\/h4>\n<p>In conclusion, database administrators and developers who want to have a neat and effective data environment will find that understanding the PostgreSQL Drop Database command is essential. Users can confidently manage the procedure and understand the implications and protections involved with dumping a database by using the examples and insights provided in this tutorial.<\/p>\n<p>To ensure the integrity of your PostgreSQL database ecosystem, as with any powerful instrument, thorough consideration and adherence to best practices are essential for a successful end. You now know two ways to drop a PostgreSQL database after completing the examples in this guide.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A robust open-source relational database management system called PostgreSQL provides a wealth of features for effectively managing and organizing data. Removing a database is one&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1560,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/1531"}],"collection":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/comments?post=1531"}],"version-history":[{"count":10,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/1531\/revisions"}],"predecessor-version":[{"id":2585,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/1531\/revisions\/2585"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/media\/1560"}],"wp:attachment":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/media?parent=1531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/categories?post=1531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/tags?post=1531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}