PostgreSQL Cheat-SheetΒΆ
PostgreSQL or also known as Postgres, is a free and open-source relational database management system. PostgreSQL features transactions with Atomicity, Consistency, Isolation, Durability (ACID) properties automatically updatable views, materialized views, triggers, foreign keys, and stored procedures. It is designed to handle a range of workloads, from single machines to data warehouses or web services with many concurrent users.
InstallationΒΆ
Install PostgreSQL 12 on Ubuntu 20.04 LTSΒΆ
Install / deploy Postgres on Kubernetes with Zalando Postgres OperatorΒΆ
Postgres is probably the database which is most common on Cloud platforms and also, running on Kubernetes environments. There are several so called "Kubernetes Operators" which handle the deployment of Postgres clusters for you. One of it is the Postgres Operator by Zalando.
You can find some tutorials regarding deployment of the operator and how to work with it, in the link list below:
- Deploy Zalando Postgres Operator on your Kubernetes cluster
- Configure Zalando Postgres Operator Backup with WAL-G
- Configure Zalando Postgres Operator Restore with WAL-G
Initial database connectionΒΆ
A local connection (from the database server) can be done by the following command:
Set password for postgres database userΒΆ
The password for the postgres
database user can be set the the quickcommand \password
or by alter user postgres password 'Supersecret'
. A connection using the postgres
user
is still not possible from the "outside" hence to the default settings in the pg_hba.conf
.
Update pg_hba.conf to allow postgres user connections with passwordΒΆ
In order to allow connections of the postgres
database user not using OS user
authentication, you have to update the pg_hba.conf
which can be found under
/etc/postgresql/12/main/pg_hba.conf
.
Change the last section of the above line to md5
.
A restart is required in order to apply the new configuration:
Now a connection from outside the database host is possible e.g.
Creation of additional database usersΒΆ
A database user can be created by the following command:
Creation of additional databasesΒΆ
One can create new Postgres databases within an instance. Therefore you can use the psql
command to login (see above).
You can leave the OWNER
section of the command, when doing so, the current user will become
owner of the newly created database.
To change the owner of an existing database later, you can use the following command:
Backup and RestoreΒΆ
There are near to endless combinations in tools and parameters to backup postgres databases. Below you can find some examples using the Postgres built-in tools pgdump
, pg_basebackup
and pg_restore
.
pg_dump / pg_dumpallΒΆ
Using pg_dump
or pg_dumpall
enables you to extract / export a PostgreSQL database(s) into a (SQL) script file or a custom archive file.
pg_dumpΒΆ
The following command creates a custom archive file from a database specified with -d
.
Using the --create
option will include the SQL commands in the dump script that will create the database before importing it later. The -Z 9
option in this example compresses the SQL script created with the highest available compression rate (0-9
).
The following command creates a custom archive file from a database specified with -d
. To export data in custom format, you have to specify so with the -F c
option. Custom file dumps have the benefit, that they are compressed by default.
Custom format files can only be restored by pg_restore
(see below). A SQL dump can be restored by using psql
.
A complete guide of pg_dump
from the official documentation can be found here.
pg_dumpallΒΆ
A full dump of all databases of a Postgres instance can be done by pg_dumpall
. It will include also user creation information.
A difference to pg_dump
, you cannot choose for different output formats. pg_dumpall
will always create a SQL script as output. Therefore,
you don't need pg_restore
for restoring a "full" dump. Only psql
is needed (see below).
If you use password authentication it will ask for a password each time. It is convenient to have a ~/.pgpass
file or PGPASSWORD
environment variable set.
So importing a full dump is really easy by the following psql
command:
A complete guide of pg_dumpall
from the official documentation can be found here.
pg_restoreΒΆ
pg_restore
can be used to restore custom file dumps created by pg_dump
.
The following command will create the database (which has been dumped before).
A complete guide of pg_restore
from the official documentation can be found here.