Sunday, September 8, 2013

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 physical location on disk.
PostgreSQL comes with two default tablespaces:
  • pg_default tablespace stores all user data.
  • pg_global tablespace stores all global data.
The tablespaces allows 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, the 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:
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 DATABASECREATE 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 physical location is 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 topostgres user by using the chwon command as the following:
It changes owner of the /usr/data/tablespace_dir directory to postgres user. Once 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.

No comments:

Post a Comment