You are on page 1of 2

Tablespace

Create tablespace:

• Syntax for creating tablespace: (ensure the location exist)


create tablespace hrd location '/opt/app/hrd/';
• Syntax for creating a table on a newly created tablespace
create table test1(studid int,stuname varchar(50))
tablespace hrd;
• Query to find which tablespace the table belong to
select * from pg_tables where tablespace='hrd';
or
select * from pg_tables where tablename=‘test1';

Move Table between tablespaces:

• Move tables from one tablespace to another


Syntax:
alter table test1 set tablespace pg_default
• Check whether the table is moved successfully to another
tablespace
Syntax: select * from pg_tables where tablename=‘test1’
• Find physical location of the table
Syntax: select pg_relation_filepath(‘test1');
• Find physical location of the tablespace
Syntax: postgres#/dt
Drop Tablespace:
• We cannot drop a tablespace which is not empty.
• Find objects associate with the tablespace
Syntax: select * from pg_tables where tablespace = 'hrd';
• Drop tablespace
Syntax: drop tablespace hrd;
• Query pg system catalog view to check the tablespace is
dropped.
Syntax: select * from pg_tablespace;

Temporary tablespace:
• How to create temporary tablespace
Syntax : CREATE TABLESPACE temp01 OWNER ownername LOCATION
'\opt\app\hrd\'
• Set temp_tablespaces=temp01 in postgresql.conf and reloaded
configuration.

You might also like