Skip to content

RootDBA

DBA expertise: PostgreSQL, SQL Server & MySQL

Menu
  • Home
  • blog
  • PostgreSql
  • MySql
  • Sql Server
  • About AHMED – Root DBA
Menu

PostgreSql tablespace

Posted on September 1, 2025September 1, 2025 by AHMED


PostgreSQL store data logically in tablespace and physically in datafiles
PostgreSQL uses a tablespace to map a logical name to a physical location on disk 

tablespace allow the user to control the disk layout of PostgreSQL
when we create table or any object it goes directory to data directly by default

to avoid load on disk we can separate the data been stored in disk to be segregated to deterrent part of the disk.

default tablespaces

default comes with two out box tablespaces namely pg_default & pg_global.
the default location for tablespaces is data directory.

pg_default

is the default tablespace for storing all user data .
is also the default tablespace for tamplate1 & tamplate2 database 

al newly created database uses pg_default tablespace , unless overridden by TABLESPACE CLAUSE while create database 

pg_global.

is the default tablespace for storing global data .

creating tablespace.

to show the tablespace that are in the db_cluster you can use the below command 

select * from pg_tablespace ;

to create new tablespace first create directory where the tablespace will point the data to be saved to 

syntax:

create tablespace [tablespace_name] location ‘[directory]’
create tablespace table1 location ‘/tablepace1’;

when creating new directory you you will need to add required permission 

chmod 700 [path] chown postgres:postgres [path]

now when you want to create any new object such as table or database or even index you can specify the table space that will store the object 

[query for creating new object] tablespace [tablespace_name]; create database testtablespace1 tablespace table1

if you check the directory of the tablespace you will find there is new file created 

move table between tablespaces

first we will check the table are assinge to which tablespace 

select * from pg_tables where tablename = 'staff';
to move table from tablespace to another we use the below syntax
first i will check in which table one target table is assinge to 

72d850fa3f5c2523279eef0dfcbc674d.png
the table store is not assinge to any tablespace so if you found the tablespace column empty it means the table is assinge to the default tablespace
alter table [table-name] set tablespace [tablespace-name];

alter table store set tablespace table1;

check the new path of the table

we have successfully move the table from default tablespace to new tablespace meaning that the table data is not located in default data path of PostgreSQL
to check and confirm that we can use the below query.

select pg_relation_filepath(‘store’);

drop tablespace

note that you cannot drop tablespace if there is object in the tablespace
the syntax :

drop tablespace [table_space_name];

Temporary Tablespace in PostgreSQL

PostgreSQL create temporary tablespace for such action you require for completing a query example sorting query
temporary tablespace deosnt store any data and they are remove after you shutdown database .

creating temporary tablespace

syntax:

create tablespace [tablespace-choosen-name] owner [username] LOCATION [filepath]

the owner parameter is not require to be added , incase you didn’t specify the owner the temp tablespace owner will be PostgreSQL .

after creating temp tablespace there is one changes must be done in PostgreSQL configuration files
there parameter called temp_tablespaces where you have to specify temp tablespace name .

0
Visited 9 times, 1 visit(s) today
Category: Uncategorized

Post navigation

← PostgreSql table inheritance , table partion
PostgreSql backup and restore →
© 2025 RootDBA | Powered by Minimalist Blog WordPress Theme