
“IF NOT EXISTS” is an optional clause that creates a new table only if it doesn’t exist already. Specify the CREATE TABLE statement followed by the table name to be created: CREATE TABLE tab_name ( Postgres offers several commands to perform different table operations, such as create table, drop table, select table’s data, etc. PostgreSQL Table Operations - Cheat Sheet OWNER TO Įxecute the “\dn” command from “psql” to get the list of available schemas: \dn Run the ALTER SCHEMA command along with the OWNER TO clause to change the schema’s owner: ALTER SCHEMA schema_name The ALTER SCHEMA command enables us to rename a schema to do that, the ALTER SCHEMA command must be executed with the RENAME TO clause: ALTER SCHEMA schema_name For instance, the ALTER SCHEMA statement allows us to rename a schema, change the schema’s owner, etc. The ALTER SCHEMA command is used in Postgres to modify the schema’s definition. Specify the schema’s name in place of “new_schema”.Ī schema can be dropped using the DROP SCHEMA statement, as shown in the following snippet: DROP SCHEMA schema_name Use the CREATE SCHEMA statement to define/create a new schema in a database: CREATE SCHEMA new_schema Postgres provides different commands to work with schemas, such as CREATE SCHEMA, DROP SCHEMA, etc. PostgreSQL Schema Operations - Cheat Sheet Use the ALTER DATABASE statement with a SET clause to override the settings of a particular database: ALTER DATABASE db_nameĮxecute the ALTER DATABASE command with RESET option to rest the configuration parameter: ALTER DATABASE db_nameĮxecute the “\l” or “\l+” command from psql to show the list of databases: \l In place of “ option”, you can specify CONNECTION LIMIT, ALLOW_CONNECTIONS, or IS_TEMPLATE. For this purpose, you must use the below syntax: ALTER DATABASE db_name Using ALTER DATABASE command, you can modify the attributes of the database, such as connection limit, connections allowed, etc. The ALTER DATABASE statement allows us to change the default tablespace of a database: ALTER DATABASE db_name Use the following syntax to rename a database in Postgres: ALTER DATABASE old_db_name OWNER TO new_owner_name | current_user |current_role | session_user

To change the database owner, users need to execute the ALTER DATABASE command as follows: ALTER DATABASE db_name This command allows us to change the database owner, rename the database, change database attributes, etc.

Use the ALTER DATABASE command to modify a particular database in Postgres. The DROP DATABASE command allows us to delete/drop a particular database: DROP DATABASE db_name Specify the name of a database to be created in place of “db_name”.Įxecute the “ \c” command(from psql) followed by the database name to which you want to establish a connection: \c db_name To create a new database in Postgres, use the CREATE DATABASE command followed by the database name to be created: CREATE DATABASE db_name To do that, different commands are used in Postgres. In PostgreSQL, you can perform various database operations, such as create, alter, drop, etc. PostgreSQL Database Operations - Cheat Sheet So, let’s start with the database operations!

PostgreSQL Table Operations - Cheat Sheet.PostgreSQL Schemas Operations - Cheat Sheet.

