Moving Tables Across Schema
postgres=# \dn;
List of schemas
Name | Owner
--------+----------
hr | postgres
public | postgres
(2 rows)
postgres=# \dt hr.*;
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
hr | bike | table | postgres
hr | vehicle | table | postgres
(2 rows)
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
public | vehicle | table | postgres
(1 row)
postgres=# alter table hr.bike set schema public;
ALTER TABLE
postgres=# \dt;
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
public | bike | table | postgres
public | vehicle | table | postgres
(2 rows)
Steps to change Schema name :
postgres=# \dn;
List of schemas
Name | Owner
--------+----------
hr | postgres
public | postgres
(2 rows)
postgres=# alter schema hr rename to hr_public;
ALTER SCHEMA
postgres=# \dn;
List of schemas
Name | Owner
-----------+----------
hr_public | postgres
public | postgres
(2 rows)
Steps to change schema owner :
postgres=# \du;
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
user_db | | {}
postgres=# \dn+
List of schemas
Name | Owner | Access privileges | Description
-----------+----------+----------------------+------------------------
hr_public | postgres | |
public | postgres | postgres=UC/postgres+| standard public schema
| | =UC/postgres |
(2 rows)
postgres=# alter schema hr_public owner to user_db;
ALTER SCHEMA
postgres=# \dn+
List of schemas
Name | Owner | Access privileges | Description
-----------+----------+----------------------+------------------------
hr_public | user_db | |
public | postgres | postgres=UC/postgres+| standard public schema
| | =UC/postgres |
(2 rows)
postgres=# \dn;
List of schemas
Name | Owner
-----------+----------
hr_public | user_db
public | postgres
(2 rows)
Droping Schema :
postgres=# drop schema hr_public cascade;
NOTICE: drop cascades to table hr_public.vehicle
DROP SCHEMA
postgres=# \dn;
List of schemas
Name | Owner
--------+----------
public | postgres
(1 row)
« Previous (Setting default schema) |