EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL Tablespaces

By Sohel SayyadSohel Sayyad

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL Tablespaces

PostgreSQL Tablespaces

Introduction to PostgreSQL Tablespace

The PostgreSQL tablespace is a physical location on a drive or disk where the PostgreSQL stores data files containing database objects like indexes and tables etc. PostgreSQL uses it in order to map logical names to a physical location on disk or drive.

It has the following two default tablespaces:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • pg_default: Stores all users data.
  • pg_global: Stores all global data.
  • How does Tablespaces work in PostgreSQL?

    • The user who executes the CREATE TABLESPACE is the owner of the PostgreSQL tablespace.
    • The SQL statement also allows us to assign the ownership of it to a different user by specifying the name in the OWNER clause.
    • The path defined under the LOCATION clause is used to create the PostgreSQL tablespace; this path is an absolute path to an empty. In order to read and write data from/to the directory, the system user must own this directory.

    How to create Tablespaces in PostgreSQL?

    Let’s illustrate the following syntax for creating a new tablespace:

    Syntax:

    CREATE TABLESPACE tablespace_name
    OWNER user_name
    LOCATION directory_path;

    Explanation:

    1. tablespace_name

    The name of the PostgreSQL tablespace to create a new PostgreSQL tablespace. We can not define the name which begins with pg_; the names starting with pg_ are reserved for PostgreSQL system tablespaces.

    2. user_name

    The name defined is the user who will own the newly created PostgreSQL tablespace.If the user_name is not defined, then the user executing the statement will own the newly created PostgreSQL tablespace. It can be created by superusers only, and they can assign the ownership of the tablespace to non-superusers.

    3. directory_path

    It will use the directory defined under the LOCATION clause. The directory path should be defined as an absolute path. Also, the defined directory should be owned by the user and must be empty.

    Let’s have a look at the following snapshot showing default tablespaces:

    default

    The following example will create a tablespace named ‘edutablespace’ at a specified location.

    CREATE TABLESPACE edutablespace
    OWNER postgres
    LOCATION 'C:\MyFolder\Articles\Tablespaces';

    Illustrate the result of the above statement with the help of the following snapshot.

    <img class="alignnone size-medium wp-image-321346" src="https://www.educba.com/academy/wp-content/uploads/2020/03/PostgreSQL-Tablespaces-2-300×133.jpg" alt="PostgreSQL Tablespaces 2" width="300" height="133" /

    How to Alter Tablespaces in PostgreSQL?

    In order to perform some alter operation on tablespace using the ALTER TABLESPACE statement, you should be a superuser or the owner of it. We can not change the location used by tablespace as it’s not supported by PostgreSQL yet.

    Consider we have a tablespace that is created, and we want to change the tablespace definition with the help of the ALTER TABLESPACE as follows:

    ALTER TABLESPACE action;

    Explanation:

    Action:

    It allows us to perform some action like rename the tablespace name, change the owner of the tablespace and set some tablespace parameters.

    1. To modify the tablespace name, we use the following statement:

    ALTER TABLESPACE tablespace_name RENAME TO new_name;

    The following statement renames edutablespace tablespace to edutablespace_new:

    ALTER TABLESPACE edutablespace RENAME TO edutablespace_new;

    Illustrate the result of the above statement with the help of the following snapshot.

    PostgreSQL Tablespaces 3

    2. To modify the tablespace owner, we use the following statement:

    ALTER TABLESPACE tablespace_name OWNER TO new_owner;

    Currently, the owner of the edutablespace_new is Postgres have a look at the following snapshot

    modify owner

    The following statement changes the owner of the edutablespace_new tablespace to ‘educba.’

    ALTER TABLESPACE edutablespace_new OWNER to educba;

    Illustrate the result of the above statement with the help of the following snapshot

    PostgreSQL Tablespaces 5JPG

    3. Modify the parameters of the tablespace.

    ALTER TABLESPACE tablespace_name SET parameter = value;

    How to delete Tablespaces in PostgreSQL?

    In order to remove it, you should be the owner or the superuser.

    Consider the following syntax:

    DROP TABLESPACE IF EXISTS tablespace_name;

    Explanation:

    Define the name of it after the DROP TABLESPACE clause.

    The IF EXISTS helps us to prevent the error of deleting a non-existent tablespace.

    We need to make sure the tablespace is empty before removing, which means verify there are no database objects inside the tablespace.

    DROP TABLESPACE edutablespace_new;

    The above statement will drop the edutablespace_new tablespace.

    Conclusion

    We hope from the above article; you have learned how to create a tablespace using CREATE TABLESPACE statement and change the tablespace’s definition with the PostgreSQL ALTER help TABLESPACE statement.

    Recommended Articles

    This is a guide to PostgreSQL Tablespaces. Here we discuss the introduction to PostgreSQL Tablespaces and how to Alter them with examples. You may also have a look at the following articles to learn more –

    1. What is PostgreSQL?
    2. PostgreSQL Primary Key | How to Work?
    3. PostgreSQL IF Statement | How to Work?
    4. PostgreSQL Auto Increment
    5. PostgreSQL Trunc() | How to Work?

    All in One Data Science Bundle (360+ Courses, 50+ projects)

    360+ Online Courses

    50+ projects

    1500+ Hours

    Verifiable Certificates

    Lifetime Access

    Learn More

    0 Shares
    Share
    Tweet
    Share
    Primary Sidebar
    PostgreSQL Tutorial
    • Basic
      • What is PostgreSQL
      • PostgreSQL Features
      • How to Install PostgreSQL
      • PostgreSQL Versions
      • PostgreSQL Architecture
      • PostgreSQL GUI
      • Postgres Command-Line
      • PostgreSQL Variables
      • PostgreSQL Data Types
      • PostgreSQL NOT NULL
      • PostgreSQL Integer
      • PostgreSQL Boolean
      • PostgreSQL BIGINT
      • PostgreSQL NULLIF
      • PostgreSQL Administration
      • PostgreSQL Commands
      • PostgreSQL Operators
      • PostgreSQL IN Operator
      • Postgres like query
      • PostgreSQL encode
      • PostgreSQL Cheat Sheet
      • PostgreSQL List Databases
      • PostgreSQL Rename Database
    • Control Statement
      • PostgreSQL IF Statement
      • PostgreSQL if else
      • PostgreSQL CASE Statement
      • PostgreSQL LOOP
      • PostgreSQL For Loop
      • PostgreSQL While Loop
    • Joins
      • Joins in PostgreSQL
      • PostgreSQL Inner Join
      • PostgreSQL Outer Join
      • LEFT OUTER JOIN in PostgreSQL
      • PostgreSQL FULL OUTER JOIN
      • PostgreSQL LEFT JOIN
      • PostgreSQL Full Join
      • PostgreSQL Cross Join
      • PostgreSQL NATURAL JOIN
      • PostgreSQL UPDATE JOIN
    • Queries
      • PostgreSQL Queries
      • PostgreSQL INSERT INTO
      • PostgreSQL WHERE Clause
      • PostgreSQL WITH Clause
      • PostgreSQL ORDER BY
      • PostgreSQL ORDER BY Random
      • PostgreSQL ORDER BY DESC
      • PostgreSQL GROUP BY
      • PostgreSQL group_concat
      • PostgreSQL HAVING
      • PostgreSQL Recursive Query
    • Advanced
      • PostgreSQL Schema
      • Postgres List Schemas
      • PostgreSQL Drop Schema
      • PostgreSQL VARCHAR
      • Array in PostgreSQL
      • PostgreSQL DDL
      • PostgreSQL List Users
      • Postgres Default User
      • Postgres add user
      • PostgreSQL User Password
      • PostgreSQL log_statement
      • PostgreSQL repository
      • PostgreSQL shared_buffer
      • PostgreSQL String Functions
      • PostgreSQL Compare Strings
      • PostgreSQL Text Search
      • PostgreSQL TEXT
      • PostgreSQL String Array
      • PostgreSQL where in array
      • PostgreSQL Constraints
      • PostgreSQL UNIQUE Constraint
      • PostgreSQL CHECK Constraint
      • PostgreSQL INTERSECT
      • PostgreSQL Like
      • Cursors in PostgreSQL
      • PostgreSQL UNION ALL
      • Indexes in PostgreSQL
      • PostgreSQL Index Types
      • PostgreSQL REINDEX
      • PostgreSQL UNIQUE Index
      • PostgreSQL Clustered Index
      • PostgreSQL DROP INDEX
      • PostgreSQL DISTINCT
      • PostgreSQL FETCH
      • PostgreSQL RAISE EXCEPTION
      • PostgreSQL Auto Increment
      • Sequence in PostgreSQL
      • Wildcards in PostgreSQL
      • PostgreSQL Subquery
      • PostgreSQL Alias
      • PostgreSQL LIMIT
      • PostgreSQL Limit Offset
      • PostgreSQL LAG()
      • PostgreSQL Table
      • Postgres Show Tables
      • PostgreSQL Describe Table
      • PostgreSQL Lock Table
      • PostgreSQL ALTER TABLE
      • Postgres Rename Table
      • PostgreSQL List Tables
      • PostgreSQL TRUNCATE TABLE
      • PostgreSQL Table Partitioning
      • Postgres DROP Table
      • PostgreSQL Functions
      • PostgreSQL Math Functions
      • PostgreSQL Window Functions
      • Aggregate Functions in PostgreSQL
      • PostgreSQL Primary Key
      • Foreign Key in PostgreSQL
      • PostgreSQL Procedures
      • PostgreSQL Stored Procedures
      • PostgreSQL Views
      • PostgreSQL Materialized Views
      • Postgres Create View
      • PostgreSQL Triggers
      • PostgreSQL DROP TRIGGER
      • PostgreSQL Date Functions
      • PostgreSQL TO_DATE()
      • PostgreSQL datediff
      • PostgreSQL Timestamp
      • PostgreSQL CURRENT_TIMESTAMP()
      • PostgreSQL Notify
      • PostgreSQL LENGTH()
      • PostgreSQL blob
      • PostgreSQL Median
      • PostgreSQL kill query
      • PostgreSQL Formatter
      • PostgreSQL RANK()
      • PostgreSQL Select
      • PostgreSQL Average
      • PostgreSQL DATE_PART()
      • PostgreSQL EXECUTE
      • PostgreSQL COALESCE
      • PostgreSQL EXTRACT()
      • PostgreSQL Sort
      • PostgreSQL TO_CHAR
      • PostgreSQL Interval
      • PostgreSQL Number Types
      • PostgreSQL ROW_NUMBER
      • Alter Column in PostgreSQL
      • PostgreSQL Identity Column
      • PostgreSQL SPLIT_PART()
      • PostgreSQL CONCAT()
      • PostgreSQL replace
      • PostgreSQL TRIM()
      • PostgreSQL MAX
      • PostgreSQL DELETE
      • PostgreSQL Float
      • PostgreSQL OID
      • PostgreSQL log
      • PostgreSQL REGEXP_MATCHES()
      • PostgreSQL MD5 
      • PostgreSQL NOW()
      • PostgreSQL RANDOM
      • PostgreSQL round
      • PostgreSQL Trunc()
      • PostgreSQL TIME
      • PostgreSQL IS NULL
      • PostgreSQL CURRENT_TIME
      • PostgreSQL MOD()
      • Postgresql Count
      • PostgreSQL Datetime
      • PostgreSQL MIN()
      • PostgreSQL age()
      • PostgreSQL enum
      • PostgreSQL OR
      • PostgreSQL Wal
      • PostgreSQL NOT IN
      • PostgreSQL SET
      • PostgreSQL Current Date
      • PostgreSQL Compare Date
      • PostgreSQL SERIAL
      • PostgreSQL UUID
      • PostgreSQL Merge
      • PostgreSQL Database
      • PostgreSQL Clone Database
      • PostgreSQL Copy Database
      • PostgreSQL Show Databases
      • PostgreSQL Restore Database
      • PostgreSQL DROP DATABASE
      • PostgreSQL ALTER DATABASE
      • Postgres DROP Database
      • Postgres Dump Database
      • PostgreSQL OFFSET
      • PostgreSQL GRANT
      • PostgreSQL COMMIT
      • PostgreSQL ROLLUP
      • PostgreSQL JSON
      • EXPLAIN ANALYZE in PostgreSQL
      • PostgreSQL Temporary Table
      • PostgreSQL Show Tables
      • PostgreSQL cluster
      • PostgreSQL Replication
      • PostgreSQL Logical Replication
      • PostgreSQL flush privileges
      • PostgreSQL Tablespaces
      • CAST in PostgreSQL
      • PostgreSQL CTE
      • hstore in PostgreSQL
      • PostgreSQL Encryption
      • PostgreSQL DECODE()
      • PostgreSQL Vacuum
      • PostgreSQL EXCLUDE
      • Postgres Change Password
      • Postgres Delete Cascade
      • PostgreSQL EXCEPT
      • PostgreSQL Roles
      • PostgreSQL Link
      • PostgreSQL Partition
      • PostgreSQL column does not exist
      • PostgreSQL Log Queries
      • PostgreSQL escape single quote
      • PostgreSQL Query Optimization
      • PostgreSQL Character Varying
      • PostgreSQL Transaction
      • PostgreSQL Extensions
      • PostgreSQL Import CSV
      • PostgreSQL Client
      • PostgreSQL caching
      • PostgreSQL Incremental Backup
      • PostgreSQL JSON vs JSONNB
      • PostgreSQL JDBC Driver
      • PostgreSQL Interview Questions
    Footer
    About Us
    • Blog
    • Who is EDUCBA?
    • Sign Up
    • Live Classes
    • Corporate Training
    • Certificate from Top Institutions
    • Contact Us
    • Verifiable Certificate
    • Reviews
    • Terms and Conditions
    • Privacy Policy
    •  
    Apps
    • iPhone & iPad
    • Android
    Resources
    • Free Courses
    • Database Management
    • Machine Learning
    • All Tutorials
    Certification Courses
    • All Courses
    • Data Science Course - All in One Bundle
    • Machine Learning Course
    • Hadoop Certification Training
    • Cloud Computing Training Course
    • R Programming Course
    • AWS Training Course
    • SAS Training Course

    © 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

    EDUCBA
    Free Data Science Course

    Hadoop, Data Science, Statistics & others

    *Please provide your correct email id. Login details for this Free course will be emailed to you

    By signing up, you agree to our Terms of Use and Privacy Policy.

    EDUCBA
    Free Data Science Course

    Hadoop, Data Science, Statistics & others

    *Please provide your correct email id. Login details for this Free course will be emailed to you

    By signing up, you agree to our Terms of Use and Privacy Policy.

    Let’s Get Started

    By signing up, you agree to our Terms of Use and Privacy Policy.

    Loading . . .
    Quiz
    Question:

    Answer:

    Quiz Result
    Total QuestionsCorrect AnswersWrong AnswersPercentage

    Explore 1000+ varieties of Mock tests View more

    EDUCBA Login

    Forgot Password?

    By signing up, you agree to our Terms of Use and Privacy Policy.

    This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

    EDUCBA

    *Please provide your correct email id. Login details for this Free course will be emailed to you

    By signing up, you agree to our Terms of Use and Privacy Policy.

    Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More