PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • Aggregate Functions
    • Date / Time Functions
    • String Functions
    • Math Functions
Home / PostgreSQL Administration / Deleting Tablespaces Using PostgreSQL DROP TABLESPACE Statement

Deleting Tablespaces Using PostgreSQL DROP TABLESPACE Statement

Summary: in this tutorial, you will learn how to remove a tablespace by using the PostgreSQL DROP TABLESPACE statement.

Introduction to DROP TABLESPACE statement

To remove a tablespace, you use DROP TABLESPACE statement as follows:

1
DROP TABLESPACE IF EXISTS tablespace_name;

You need to specify the name of the tablespace after the DROP TABLESPACE clause. The IF EXISTS helps you avoid the error of removing a non-existent tablespace.

Only tablespace owner or the superuser can delete the tablespace.

Before deleting the tablespace, make sure that it is empty, which means there are no database objects inside it.

PostgreSQL DROP TABLESPACE example

First, create a new tablespace named demoand maps it to the c:\data\demo directory.

1
CREATE TABLESPACE demo LOCATION 'c:/data/demo';

Second, create a new database named dbdemoand set its tablespace to demo:

1
CREATE DATABASE dbdemo TABLESPACE = demo;

Third, create a new table named testin the dbdemoand set it tablespaceto demo:

1
2
3
4
CREATE TABLE test (
ID serial PRIMARY KEY,
title VARCHAR (255) NOT NULL
) TABLESPACE demo;

You can get all objects in the demotablespace by using the following query:

1
2
3
4
5
6
7
8
SELECT
ts.spcname,
cl.relname
FROM
pg_class cl
JOIN pg_tablespace ts ON cl.reltablespace = ts.oid
WHERE
ts.spcname = 'demo';

PostgreSQL Drop Tablespace example

Fourth, try to delete the demotablespace:

1
DROP TABLESPACE demo;

We got an error message:

1
[Err] ERROR: tablespace "demo" is not empty

Because the demo tablespace is not empty, we could not delete it.

Fifth, login to the postgresdatabase and delete the dbdemodatabase:

1
DROP DATABASE dbdemo;

Instead of deleting the database, you can move it to the another tablespace e.g.,  pg_default by using the ALTER TABLE statement as follows:

1
2
ALTER DATABASE dbdemo
SET TABLESPACE = pg_default;

Sixth, delete the demotablespace again:

1
DROP TABLESPACE demo;

It worked. The demo tablespace was deleted.

In this tutorial, we have shown you how step by step how to delete tablespace by using the PostgreSQL DROP TABLESPACE statement.

Previous Tutorial: Reset Forgotten Password For postgres User
Next Tutorial: How To Change The Password of a PostgreSQL User

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

Databases Management

  • Create New Databases
  • Modify Databases
  • Delete Databases
  • Copy a Database
  • Get Database Object Sizes

PostgreSQL Roles Administration

  • Introduction to PostgresQL Roles

PostgreSQL Backup & Restore

  • PostgreSQL Backup Databases
  • PostgreSQL Restore Databases

Tablespaces Management

  • Creating Tablespaces
  • Changing Tablespaces
  • Deleting Tablespaces

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL ANY Operator
  • PostgreSQL EXISTS
  • How To Delete Duplicate Rows in PostgreSQL
  • PostgreSQL TO_CHAR Function
  • PostgreSQL TO_NUMBER Function
  • PostgreSQL TO_TIMESTAMP Function
  • PostgreSQL CEIL Function
  • PostgreSQL MOD Function
  • PostgreSQL FLOOR Function
  • PostgreSQL ABS Function

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.