PostgreSQL Tutorial

  • Home
  • Administration
  • Views
  • Triggers
  • Stored Procedures
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
Home / PostgreSQL Administration / PostgreSQL Creating Tablespaces

PostgreSQL Creating Tablespaces

Summary: in this tutorial, you will learn how to create tablespaces by using the PostgreSQL CREATE TABLESPACE statement.

Introduction to PostgreSQL tablespace

A tablespace is a location on disk where PostgreSQL stores data files containing database objects e.g., indexes., tables, etc. PostgreSQL uses a tablespace to map a logical name to a physical location on disk.

PostgreSQL comes with two default tablespaces:

  • pg_defaulttablespace stores all user data.
  • pg_globaltablespace stores all global data.

The tablespaces allow you to control the disk layout of PostgreSQL. There are two main advantages of using tablespaces:

  • First, if a partition on which the cluster was initialized is out of space, you can create a new tablespace on a different partition and use it until you reconfigure the system.
  • Second, you can use the statistics of database objects’ usages to optimize the performance of databases. For example, you can place the frequent access indexes or tables on devices that perform very fast e.g., solid state devices, and put the tables containing archive data which is rarely used on a slower devices.

PostgreSQL CREATE TABLESPACE statement

To create new tablespaces, you use CREATE TABLESPACE statement as follows:

1
2
3
CREATE TABLESPACE tablespace_name
OWNER user_name
LOCATION directory_path;

The name of the tablespace should not begin with pg_, because these names are reversed for the system tablespaces.

By default, the user who executed the CREATE TABLESPACE is the owner of the tablespace. The statement also allows assign the ownership of tablespace to another user specified in the ONWER clause.

The directory_path is the absolute path to an empty directory used for the tablespace. PostgreSQL system user must own this directory in order to read and write data into it.

Once a tablespace is created, you can specify it in the CREATE DATABASE, CREATE TABLE and CREATE INDEX statements to store data files of the objects in the tablespace.

PostgreSQL create tablespace examples

The following statement creates a new tablespace named dvdrental that has the physical location is c:/data/dvdrental.

1
CREATE TABLESPACE dvdrental LOCATION 'c:\data\dvdrental';

Notice that we used the Unix-style slashes for the directory path.

To create new tablespace in pgAdmin, follow the steps below:

First, from the Tablespaces, right mouse click and choose New Tablespaces… menu item. A new Window will display.

PostgreSQL create tablespace pgAdmin

Second, enter the name and owner of the tablespace in the properties tab, as well as the location in the Definition tab.

Specify tablespace name and owner

Specify tablespace location

Click on the SQL tab, you will see the CREATE TABLESPACE command in detail.

Tablespace SQL Preview

Third, click OK button to create the tablespace.

dvdrental tablespace

Creating tablespace in UNIX

When you create a tablespace in UNIX systems, a permission error may occur even 777 permission granted to the tablespace directory. To fix this issue, you need to change to owner of the data directory to postgresuser by using the chwoncommand as the following:

1
# chown postgres /usr/data/tablespace_dir

It changes the owner of the /usr/data/tablespace_dir directory to postgresuser. Once the postgres user takes over data directory ownership, it will remove all other accesses e.g., 700.

In this tutorial, we have shown you how to create new tablespaces by using the PostgreSQL CREATE TABLE statement.

Previous Tutorial: Backing Up Databases Using PostgreSQL Backup Tools
Next Tutorial: PostgreSQL ALTER TABLESPACE

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

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 Recursive View
  • Learn PostgreSQL Recursive Query By Example
  • Creating Updatable Views Using the WITH CHECK OPTION Clause
  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
  • How to Generate a Random Number in A Range
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Boolean Data Type with Practical Examples
  • Understanding PostgreSQL Timestamp Data Types

More Tutorials

  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Functions
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

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