githubEdit

postgreSQL_cheatsheet

\PostgreSQL commands\\\

Create a new rolearrow-up-right:

CREATE ROLE role_name;

Create a new role with a username and password:

CREATE ROLE username NOINHERIT LOGIN PASSWORD password;

Change role for the current session to the new_role:

SET ROLE new_role;

Allow role_1 to set its role as role_2:

GRANT role_2 TO role_1;

===========================

Managing databases

=========================== Create a new databasearrow-up-right:

CREATE DATABASE [IF NOT EXISTS] db_name;

Delete a database permanentlyarrow-up-right:

DROP DATABASE [IF EXISTS] db_name;

===========================

Managing tables

=========================== Create a new tablearrow-up-right or a temporary tablearrow-up-right

Add a new columnarrow-up-right to a table:

Drop a columnarrow-up-right in a table:

Rename a columnarrow-up-right:

Set or remove a default value for a column:

Add a primary key arrow-up-rightto a table.

Remove the primary key from a table.

Rename a tablearrow-up-right.

Drop a tablearrow-up-right and its dependent objects:

===========================

Managing views

=========================== Create a viewarrow-up-right:

Create a recursive viewarrow-up-right:

Create a materialized viewarrow-up-right:

Refresh a materialized view:

Drop a view:

Drop a materialized view:

Rename a view:

===========================

Managing indexes

=========================== Creating an index with the specified name on a table

Removing a specified index from a table

===========================

Querying data from tables

=========================== Query all data from a table:

Query data from specified columns of all rows in a table:

Query data and select only unique rows:

Query data from a table with a filter:

Assign an aliasarrow-up-right to a column in the result set:

Query data using the LIKEarrow-up-right operator:

Query data using the BETWEENarrow-up-right `` operator:

Query data using the INarrow-up-right `` operator:

Constrain the returned rows with the LIMITarrow-up-right clause:

Query data from multiple using the inner joinarrow-up-right, left joinarrow-up-right, full outer joinarrow-up-right, cross joinarrow-up-right and natural joinarrow-up-right:

Return the number of rows of a table.

Sort rows in ascending or descending order:

Group rows using GROUP BYarrow-up-right clause.

Filter groups using the HAVINGarrow-up-right clause.

===========================

Set operations

=========================== Combine the result set of two or more queries with UNIONarrow-up-right operator:

Minus a result set using EXCEPTarrow-up-right operator:

Get intersection of the result sets of two queries:

===========================

Modifying data

=========================== Insert a new row into a tablearrow-up-right:

Insert multiple rows into a table:

Updatearrow-up-right data for all rows:

Update data for a set of rows specified by a condition in the WHERE clause.

Delete all rowsarrow-up-right of a table:

Delete specific rows based on a condition:

Performance

Show the query plan for a query:

Show and execute the query plan for a query:

Collect statistics:

Last updated