Create database with user

sudo -u postgres createuser USER sudo -u postgres createdb DB sudo -u postgres psql alter user USER with encrypted password 'PASS'; alter database DB owner to USER;

Restore after messing up a database

This is an example of doing a quick and dirty restore of a database. Typically happens after doing something stupid, like running UPDATE without a WHERE (oops). In this case, we restore database iix-notes (which by pure coincidence is the same name as this site) from a daily backup. The database is hosted on a postgresql docker container, but it should work the same regardless. Fetch a backup and copy it to the docker container cd $HOME gpg -o iix-notes....

Backup docker postgresql data with crontab

# m h dom mon dow command # SQL Backup (daily) 15 1 * * * docker exec -t iixnotes_database_1 pg_dumpall -U postgres | gzip > /media/stor/backup/iix-notes.daily.psql.gz # SQL Backup (weekly) 15 1 * * 0 docker exec -t iixnotes_database_1 pg_dumpall -U postgres | gzip > /media/stor/backup/iix-notes.weekly.psql.gz # SQL Backup (monthly) 15 1 1 * * docker exec -t iixnotes_database_1 pg_dumpall -U postgres | gzip > /media/stor/backup/iix-notes.monthly.psql.gz

Rolling up sequence numbers

Find sequences: \c database_name \ds For each, run: SELECT setval('your_table_id_seq', (SELECT MAX(id) FROM your_table));