====== PostgreSQL ======
===== Misc =====
==== Basic commands session ====
dovahkiin@RPi64:~ $ docker exec -it FT_PostgreSQL /bin/bash
postgres@ftpostgresql:/$ psql
psql (16.15 (Debian 16.15-1.pgdg12+2))
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | ICU Locale | ICU Rules | Access privileges
---------------+----------+----------+-----------------+------------+------------+------------+-----------+-----------------------
debugger | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | |
firefly | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | =Tc/postgres +
| | | | | | | | postgres=CTc/postgres+
| | | | | | | | firefly=c/postgres
fueltracker | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | |
fuelwatcher | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | |
isithotinhere | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | |
postgres | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | |
template0 | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
wnmcshop | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | |
(9 rows)
postgres=# \c fueltracker
You are now connected to database "fueltracker" as user "postgres".
fueltracker=# \dn
List of schemas
Name | Owner
-------------+-------------------
fuelarchive | postgres
fueltracker | postgres
public | pg_database_owner
(3 rows)
fueltracker=# \dt fueltracker.*
List of relations
Schema | Name | Type | Owner
-------------+--------------+-------+----------
fueltracker | brand | table | postgres
fueltracker | csv_import | table | postgres
fueltracker | date_price | table | postgres
fueltracker | fw_brand | table | postgres
fueltracker | product | table | postgres
fueltracker | region | table | postgres
fueltracker | state_region | table | postgres
fueltracker | station | table | postgres
(8 rows)
fueltracker=# \dt fuelarchive.*
List of relations
Schema | Name | Type | Owner
-------------+--------------+-------+----------
fuelarchive | brand | table | postgres
fuelarchive | csv_import | table | postgres
fuelarchive | date_price | table | postgres
fuelarchive | fw_brand | table | postgres
fuelarchive | loaded_files | table | postgres
fuelarchive | product | table | postgres
fuelarchive | region | table | postgres
fuelarchive | state_region | table | postgres
fuelarchive | station | table | postgres
(9 rows)
==== Copy Schema to other Database ====
# Include data
pg_dump -d fuelarchive -n fuelarchive | psql -d fuelwatcher
# Schema only
pg_dump -d fuelarchive --schema-only -n fuelarchive | psql -d fuelwatcher
===== DEV refresh =====
==== Backup fuelwatcher on PROD ====
docker exec -it FT_PostgreSQL /bin/bash
cd /var/lib/postgresql/data/backups/
pg_dump -d fuelwatcher > fuelwatcher_RPi64_`date +%Y-%m-%dT%H-%M-%S`.backup
Login to root:
sudo su -
rsync -av /var/lib/docker/volumes/fueltracker_postgres-data/_data/backups/ /media/usb/backup/postgresql/backups/
==== Restore on DEV ====
Copy //daily// backup from PROD to DEV using Beyond Compare config **Docker/backups (WSL <--> KARGATH)**
Login:
docker exec -it FT_PostgreSQL /bin/bash
su - postgres
psql
DROP and CREATE DATABASE
select pg_terminate_backend(pid) from pg_stat_activity where datname='fuelwatcher';
DROP DATABASE fuelwatcher;
CREATE DATABASE fueltracker
WITH
OWNER = postgres
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.utf8'
LC_CTYPE = 'en_US.utf8'
TABLESPACE = pg_default
CONNECTION LIMIT = -1;
Backup DEV then Restore PROD to DEV:
cd /var/lib/postgresql/data/backups/
pg_dump -d fueltracker > fueltracker_FALKREATH_`date +%Y-%m-%dT%H-%M-%S`.backup
psql -d fuelwatcher < fueltracker_RPi64_2021-08-01T05-23-25.backup
TMP
pg_dumpall > dev-container_devcontainer_db_1_all_`date +%Y-%m-%dT%H-%M-%S`.backup
cd /workspace/FuelTracker/data/STONARD\ backups/
cd /workspace/FuelTracker/data/RPi-WiFi\ backups/
===== Data Dictionary export =====
For comparison between PROD and DEV. Perform on both:
pg_dump -Cs fueltracker > /workspace/FuelTracker/backups/fueltracker_FALKREATH_`date +%Y-%m-%dT%H-%M-%S`.bak
===== Version upgrade =====
Test (gunzipped) restore of daily PROD backup to test PostgreSQL server (new version).
docker exec -it TEST_PostgreSQL /bin/bash
cd /var/lib/postgresql/data/backups/
psql -U postgres -f FW_PostgreSQL_all_2023-11-28T00-00-01.bak
Stop both PostgreSQL servers.
Move PROD directory aside and clone test data files into place (perms are important).
mv _data data_bak
rsync -av ../postgresql-test_test-postgres-data/_data .
Move backups back to proper location
mv data_bak/backups/* _data/backups/
===== Password fix =====
SELECT
rolname, rolpassword ~ '^SCRAM-SHA-256\$' AS has_upgraded
FROM pg_authid
WHERE rolcanlogin;
\password postgres
===== References =====
[[https://www.postgresqltutorial.com/postgresql-administration/postgresql-restore-database/|PostgreSQL Restore Database]]
[[https://blog.crunchydata.com/blog/how-to-upgrade-postgresql-passwords-to-scram|How to Upgrade Your PostgreSQL Passwords to SCRAM ]]
[[https://www.chrisnewland.com/postgres-dump-database-schema-with-or-without-data-61|Postgres dump database schema with or without data]]
[[https://www.commandprompt.com/education/postgresql-basic-psql-commands/|PostgreSQL: Basic psql Commands]]
[[https://www.kostolansky.sk/posts/upgrading-to-postgresql-15/|How to upgrade PostgreSQL from 14 to 15]]