PostgreSQL Reference

Kip Landergren

(Updated: )

Contents

Install Location / Data Directory / Data Area

macOs /Library/PostgreSQL/16/
Debian /var/lib/postgresql/

Postgres Utilities

createdb

Creates a new PostgreSQL database. The database user who executes this command becomes the owner of the new database, unless -O/--owner is specified.

createdb [connection-option...] [option...] [dbname [description]]
-U --username connect as the postgres user username
-O --owner specifies the database user who will own the new database
-e --echo echo the commands that createdb generates and sends to the server
createdb --username postgres --echo mydb
Password:
SELECT pg_catalog.set_config('search_path', '', false);
CREATE DATABASE mydb;

dropdb

Destroys an existing PostgreSQL database. Wrapper around DROP DATABASE.

dropdb [connection-option...] [option...] dbname

initdb

Creates a new PostgreSQL database cluster. Use this to create the directories where cluster data will live, etc.

From the initdb(1) man page:

initdb must be run as the user that will own the server process, because the server needs to have access to the files and directories that initdb creates. Since the server cannot be run as root, you must not run initdb as root either. (It will in fact refuse to do so.)

--pwprompt prompts for a password to give the bootstrap superuser
-A --auth the default authentication method for local users used in pg_hba.conf
-D --pgdata the directory where the database cluster should be stored
-k --data-checksums use checksums on data pages to help detect corruption by the I/O system (may incur noticeable performance penalty)

For macOS:

mkdir path/to/data
chown -R postgres:daemon path/to/data
sudo -u postgres initdb --data-checksums --auth scram-sha-256 --pwprompt --pgdata path/to/data

Notes:

pg_config

pg_config provides information about the installed version of PostgreSQL.

pg_config [OPTION]...

With no arguments, all known items are shown.

--sysconfdir show location of system-wide configuration files

pg_ctl

Initialize, start, stop, or control a PostgreSQL server.

-D --pgdata the directory where the database cluster should be stored
-l --log (strongly recommended) append the server log output to filename
-s --silent print only errors
sudo -u postgres pg_ctl init --pgdata path/to/data
sudo -u postgres pg_ctl start --pgdata path/to/data --log path/to/postgres.log
sudo -u postgres pg_ctl stop --pgdata path/to/data

pg_dump

Therefore, it is wise to run ANALYZE after restoring from a dump file to ensure optimal performance; see Section 24.1.3 and Section 24.1.6 for more information.

pg_restore

postgres

PostgreSQL database server

-D the file system location of the database configuration files

psql

PostgreSQL interactive terminal. Reads from ~/.psqlrc on startup, if it exists.

-U --username connect as the postgres user username
-c --command command to execute
-d --dbname database name to connect to
-f --file execute commands from file, then exit
-h --host host name to connect to
-s --single-step user will be prompted before each command is sent to server

Connect to the local cluster as user postgres:

psql --username postgres

Connect to the local cluster as user postgres, connecting to database mydb:

psql --username postgres --dbname mydb

postgres-common Utilities

pg_conftool

pg_createcluster

pg_ctlcluster

Start, stop, restart, reload a PostgreSQL cluster.

You have to start this program as the user who owns the database cluster or as root.

status checks whether a server is running
stop stops the server of a given cluster
start starts a server and creates a log file, if needed
restart stops the server (if it is running) and starts it again
reload reread configuration files without a full shutdown of the server

pg_dropcluster

pg_lsclusters

pg_renamecluster

pg_upgradecluster

pg_virtualenv

3rd Party Tools

pgbackrest

RTO vs. RPO is a great way to frame the problem

Selected Global Options

--config-include-path
--config
--log-level-console
--log-path
--stanza

backup

|---------------------|---| | --type | |

Create a diff backup:

sudo -u postgres pgbackrest             \
  --config=/path/to/pgbackrest.conf     \
  --config-include-path=/path/to/conf.d \
  --log-level-console=info              \
  --log-path=/path/to/log/pgbackrest    \
  --stanza=demo                         \
  --type=diff

info

Gives info on current state of backups for a stanza:

sudo -u postgres pgbackrest             \
  --config=/path/to/pgbackrest.conf     \
  --config-include-path=/path/to/conf.d \
  --log-level-console=info              \
  --stanza=demo                         \
  info

pg_format

SQL formatter / beautifier.

Selected Options

-b --comma-start in a parameters list, start with the comma (see -e)
-e --comma-end in a parameters list, end with the comma (default)

Meta-Commands

Help
help show help
\q quit
\h help with SQL commands
\? help with psql commands
Options
S show system options
+ show additional detail
Informational
\list[+] or \l[+] list databases
\dt[S+] [PATTERN] list tables (describe tables)
Connection
\connect or \c connect to a database

List tables in mydb:

mydb=# \dt
          List of relations
 Schema |  Name   | Type  |  Owner
--------+---------+-------+----------
 public | cities  | table | postgres
 public | weather | table | postgres
(2 rows)

List tables in mydb with additional detail:

mydb=# \dt+
                                      List of relations
 Schema |  Name   | Type  |  Owner   | Persistence | Access method |    Size    | Description
--------+---------+-------+----------+-------------+---------------+------------+-------------
 public | cities  | table | postgres | permanent   | heap          | 8192 bytes |
 public | weather | table | postgres | permanent   | heap          | 8192 bytes |
(2 rows)

Environment Variables

PGDATA file system location of the database configuration files
PGHOST name of the host to connect to; if it looks like an absolute path then this is the directory in which the socket file is stored
PGUSER postgres user name to connect as; defaults to the same as the operating system name of the user running the application
PGDATABASE database name; defaults to be the same as the user name

Tips

On role names:

Resources

Frequently Asked Questions (FAQs)

Why does INSERT return lines INSERT 0 1?

This is a command tag of the form:

INSERT oid count

Where oid refers to the deprecated object identifier and count refers to the number of rows inserted. See more detail in the INSERT documentation.

Why is it called “Postgres”? Where did the name “Postgres” come from? Where did the name “PostgreSQL” originate?

Chronological sequence:

How to stop the postgres process and disable it from running at startup on macOS?

Determine the .plist defining the launchd service:

ls -halt /Library/LaunchDaemons/

Let’s assume it is /Library/LaunchDaemons/postgresql-16.plist.

Tell launchd to stop the service and remove it from the current boot session:

sudo launchctl bootout system /Library/LaunchDaemons/postgresql-16.plist

Tell launchd to prevent the service from running at next boot session:

sudo launchctl disable system/postgresql-16

Confirm it worked:

sudo launchctl print-disabled system

How can I tell what locale my database is in?

SELECT datname, datcollate, datctype
FROM pg_database
WHERE datname = current_database();

Database Management System (DBMS) Terminology

atomic
either happens completely or not at all
catalog
database; used in the SQL standard
cluster
the collection of databases managed by a single PostgreSQL server instance
dump
generate a file with SQL commands that, when fed back into the server, will recreate the database in the same state as it was at the time of the dump
durability
a guarantee of the recording of committed transactions even if the server crashes or loses power
function, window function
performs a calculation across a set of table rows that are somehow related to the current row
join
Queries that access multiple tables (or multiple instances of the same table) at one time
partition
splitting a single table into smaller tables
qualify
adding more specificity to a field or query; e.g. using SELECT tbl1.foo instead of SELECT foo; or adding a clause to a query (like WHERE) that restricts the results
query
how to retrieve data from a table

the process of retrieving, or the command to retrieve, data from a database

relation
essentially a mathematical term for a table
table
a named collection of rows
table expression
an expression that computes a table
transaction
bundles multiple steps into a single, all-or-nothing operation
transaction block
a group of statements surrounded by BEGIN and COMMIT
update
modifying data that is already present in the database
xact
short for “transaction”

postgres-study

README.md

# postgres-study

personal study area for Postgres

queries/delete.sql

DELETE FROM t1
WHERE
  col_a = 1;

queries/insert.sql

INSERT INTO t1 (col_a)
VALUES
('foo')
, ('bar')
, ('baz');

queries/select-like.sql

SELECT col_a
FROM
  t1
WHERE
  col_a LIKE '%pattern%';