Articles

PostgreSQL: moving rows to an archive table and passwordless psql

· 1 min readpostgresqlsqlsoftdevel

Move rows to an archive table in one query

A DELETE ... RETURNING inside a WITH query passes the deleted rows straight to an INSERT:

WITH moved_rows AS (
    DELETE FROM box_magldno.serialno
    WHERE tms::DATE <= '2018-12-31'
    RETURNING *
)
INSERT INTO box_magldno.serialno_2018
SELECT * FROM moved_rows;

Documentation: WITH Queries

Table partitioning

Besides declarative partitioning, PostgreSQL supports partitioning with table inheritance:

psql without typing the password

Store the credentials in ~/.pgpass (format host:port:database:user:password):

$ cat ~/.pgpass
localhost:5432:*:postgres:pass

psql ignores the file unless its permissions are 0600 (chmod 600 ~/.pgpass).

Set the default host in ~/.zshrc:

export PGHOST=localhost

To run PostgreSQL itself in a container, see Podman: PostgreSQL in a container.

All articles

See also

softdevelPL

Notuj swobodnie, niech AI zadba o resztę

Od zawsze, im więcej notowałem, tym mniej użyteczny stawał się mój notes, czy to papierowy, czy elektroniczny.

linuxsoftdevel

Linux server administration: a practical cheatsheet

Security checks, certificates, scheduled jobs, disks and backups, network diagnostics and basic system configuration — commands I use on Linux servers.

ovhlinuxsoftdevel

OVH Public Cloud: moving instances, adding disks and swapping the gateway

Moving virtual servers with the OpenStack client, attaching a new block storage disk, and replacing the private network gateway when the public IP is blacklisted.