How to create tablespace in PostgreSQL with examples

In this article, we will see How to create tablespace in PostgreSQL with examples and other actions can be performed related to PostgreSQL tablespace.

What is PostgreSQL tablespace?

A tablespace is a location on the disk where PostgreSQL stores data files containing database objects e.g., tables, and indexes. PostgreSQL uses a tablespace to map a logical name to a physical location on disk.

PostgreSQL comes with two default tablespaces:
pg_default tablespace stores user data.
pg_global tablespace stores global data.

Syntax to Create PostgreSQL Tablespace:

CREATE TABLESPACE tablespace_name [ OWNER owner_name] LOCATION 'directory'

Continue reading How to create tablespace in PostgreSQL with examples

How to find list of databases and their sizes in PostgreSQL

In this article, we will see how to find list of databases and their sizes in PostgreSQL.

Find the list of databases in PostgreSQL Server.

We can find the databases by using:

1. with meta-command
2. By querying against pg_database table.

Continue reading How to find list of databases and their sizes in PostgreSQL

How to find PostgreSQL table size and number of rows with single query

In this article, we will see how to find PostgreSQL table size and number of rows with single query. We can the table size and number of rows by querying against the table pg_stat_user_tables.

Query to find PostgreSQL table size and number of rows
Following query will return the PostgreSQL table size and number of rows of that table.

select pg_relation_size(relid) as tablesize,schemaname, n_live_tup from pg_stat_user_tables where relname='table_name';

Example to find PostgreSQL table size and number of rows
Continue reading How to find PostgreSQL table size and number of rows with single query

How to find the table size in PostgreSQL

In this article, we will see how to find the table size in PostgreSQL. We can find the table size by using pg_relation_size. pg_relation_size returns the size of a table in bytes. If we want more readable format, use pg_size_pretty() function.

We can also find the size of PostgreSQL table, using meta command \dt+ table_name.

Find the table size in PostgreSQL examples:

1. Find the PostgreSQL table size using pg_relation_size.
Continue reading How to find the table size in PostgreSQL

How to find database size in PostgreSQL

In this article, we will see how to find database size in PostgreSQL server. We can find database size in PostgreSQL by using SQL Statement and size of the files that make up the PostgreSQL database server.

1. Find the size of current database in PostgreSQL server.

select pg_size_pretty(pg_database_size(current_database()));

Continue reading How to find database size in PostgreSQL