Open In App

PostgreSQL – Cheat Sheet

PostgreSQL is a powerful, open-source object-relational database system that aimed to help developers build applications, administrators to protect data integrity and build fault-tolerant environments. It supports advanced data types and performance optimization features, like Ms-SQL Server and Oracle.

Features of PostgreSQL:

Data types in PostgreSQL:

The following table list all the data types available in PostgreSQL:



PostgreSQL Data Types

Numeric Character Date/Time Monetary Binary
Boolean Geometric JSON Enumerated Text-Search
UUID Network Address Types Composite Object Identifiers Pseudo
BitString XML Range Arrays pg_lsn

Operators in PostgreSQL:

An operator manipulates individual data items and returns a result. These are the reserved words used in WHERE clause to perform operations.

Operators

Arithmetic Operators +, -, *, /, %, ^, !
Comparison Operators =, !=, <>, >, <, >=, <=
Logical Operators AND, NOT, OR
Bitwise Operators &, |

Installation:

sudo apt install postgresql

OR

sudo apt install postgresql postgresql-contrib

The postgresql-contrib will add some additional utilities and functionalities.



sudo -i -u postgres

Working With Databases:

The CREATE DATABASE command is used to create the database.

Syntax:

CREATE DATABASE database_name;

Example:

Create Database

Now, to store the data, we need the table. The CREATE TABLE statement is used to create the table.

Syntax:

CREATE TABLE table_name
(column_1 data_type,
column_2 data_type,
...
column_n data_type);

Example: 

Create table

The INSERT command is used to insert the new record (row) into the table.

Syntax: 

INSERT INTO table_name (column_1, column_2 ,...)
VALUES(value_1, value_2, ...);

Example:  

Insert into table

The SELECT statement is used to fetch the data from a database table, which returns the data in the form of a result table.

Syntax: 

SELECT  
column_1, column_2, .. column_n
FROM
table_name;

Example:

Select command

Note: We can fetch all the fields of the table using an asterisk(*) sign.

The WHERE clause is used to filter results returned by the SELECT statement.

Syntax:

SELECT column_name
FROM 
table_name 
WHERE condition;

Example:

Where clause

The LIMIT clause is used to get a subset of rows generated by a query. It is an optional clause of the SELECT statement. OFFSET n skips ‘n’ rows before beginning to return rows.

Syntax:

SELECT * 
FROM table_name 
LIMIT n;

Example:

Limit & Offset

The structure of an existing table can be modified using the ALTER TABLE statement. PostgreSQL supports the various actions to perform with ALTER TABLE as listed below:

ALTER TABLE table_name ADD COLUMN new_column_name TYPE;
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE table_name RENAME COLUMN column_name TO new_column_name;
ALTER TABLE table_name ALTER COLUMN column_name [SET DEFAULT value | DROP DEFAULT];
ALTER TABLE table_name ALTER COLUMN column_name [SET NOT NULL| DROP NOT NULL];
ALTER TABLE table_name ADD CHECK expression;
ALTER TABLE table_name ADD CONSTRAINT constraint_name constraint_definition;
ALTER TABLE table_name RENAME TO new_table_name;

The UPDATE statement is used to update or modify existing data in the table.

Syntax:

UPDATE table_name
SET column_1 = value_1,
   column_2 = value_2, ...
WHERE
   condition_1 AND condition_2;

Example:

Update

The DELETE statement is used to delete the row(s) from the table.

Syntax:

DELETE FROM table_name
WHERE condition;

Example:

Delete

Article Tags :