PostgreSQL - CREATE TABLESPACE
Last Updated :
08 Aug, 2024
In PostgreSQL, a tablespace is a mechanism used to map a logical name to a physical location on disk. Simply put, a tablespace is a disk location where PostgreSQL stores all its database objects, such as tables, indexes, and more. Understanding and utilizing tablespaces effectively can significantly enhance the management and performance of your PostgreSQL database.
What is a Tablespace in PostgreSQL?
A tablespace in PostgreSQL provides a way to control the disk layout of a database by specifying where the data files should be stored. This feature allows you to optimize storage and performance by distributing data across different physical locations.
Default Tablespaces in PostgreSQL
PostgreSQL comes with two default tablespaces:
- pg_default: Used to store user data.
- pg_global: Used to store global data.
Advantages of Using Tablespaces
Tablespaces in general are used to manage and control the disk layout of PostgreSQL. There are two main advantages of using tablespaces:
- It comes in handy when an initialized cluster in a partition runs out of space. The tablespace can here further be used to create a new tablespace in a different partition altogether until your configuration is adjusted for the lack of space on the previous partition.
- The database performance can be optimized using tablespaces.
Syntax
CREATE TABLESPACE tablespace_name
OWNER user_name
LOCATION directory_path;
Parameters
- tablespace_name: The name of the tablespace.
- user_name: The owner of the tablespace.
- directory_path: The physical location on disk.
Note: The name of the tablespace must not start with 'pg_
'
, as these prefixes are reserved for system tablespaces.
PostgreSQL CREATE TABLESPACE Example
The following statement uses the CREATE TABLESPACE to create a new tablespace called 'gfg' with the physical location 'c:\data\gfg'.
CREATE TABLESPACE gfg
LOCATION 'C:\data\gfg';
To list all tablespaces in the current PostgreSQL database server, the following command can be used:
\db
Output:

The below command shows more information such as size and access privileges:
\db+
The result will be similar to the image depicted below:

This command provides a comprehensive view of all tablespaces, including their properties and usage.
Best Practices for Using Tablespaces
- Do not use 'pg_' as a prefix for your tablespace names to avoid conflicts with system tablespaces.
- Place frequently accessed tables and indexes on faster disks to enhance performance.
- Regularly monitor the space usage of each tablespace to prevent running out of space unexpectedly.
- Ensure that your backup and recovery procedures account for the physical locations specified by your tablespaces.
Similar Reads
PostgreSQL - ALTER TABLESPACE In PostgreSQL, a tablespace is a storage location where database objects like tables and indexes are stored. Managing tablespaces effectively is crucial for optimizing performance and resource allocation in your database environment. PostgreSQL provides the ALTER TABLESPACE statement, allowing you t
2 min read
PostgreSQL - CREATE TABLE In PostgreSQL, the CREATE TABLE statement is used to define a new table within a database. It allows us to specify the table's structure, including column names, data types, and constraints, ensuring data integrity and consistency. Understanding the PostgreSQL table creation process is essential for
5 min read
PostgreSQL - DROP TABLESPACE In PostgreSQL, the DROP TABLESPACE statement is used to remove a tablespace. A tablespace is a storage location where PostgreSQL stores database objects such as tables and indexes. This command is useful for managing and cleaning up database storage.Let us get a better understanding of the DROP TABL
2 min read
PostgreSQL - CREATE TABLE AS The CREATE TABLE AS statement in PostgreSQL is a powerful tool used to create a new table and populate it with data returned by a query. This functionality allows you to generate tables on the fly based on query results, which can be very useful for reporting, analysis, and other tasks.Let us better
3 min read
PostgreSQL - CREATE SCHEMA PostgreSQL provides the CREATE SCHEMA statement to create a new schema in a database. By creating schemas, users can effectively separate data into logical groups, making it easier to manage and access information. Schemas also enhance security by controlling object visibility and permissions, allow
5 min read