This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| postgresql [2022/10/16 23:30] – admin | postgresql [2026/09/06 14:39] (current) – [Copy Schema to other Database] admin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== PostgreSQL ====== | ====== PostgreSQL ====== | ||
| - | ===== Password fix ===== | + | ===== Misc ===== |
| - | < | + | ==== Basic commands session |
| - | SELECT | + | < |
| - | rolname, rolpassword | + | dovahkiin@RPi64: |
| - | FROM pg_authid | + | postgres@ftpostgresql: |
| - | WHERE rolcanlogin; | + | psql (16.15 (Debian 16.15-1.pgdg12+2)) |
| - | </code> | + | Type " |
| - | < | + | |
| - | \password postgres | + | |
| - | </ | + | |
| + | postgres=# \l | ||
| + | List of databases | ||
| + | | ||
| + | ---------------+----------+----------+-----------------+------------+------------+------------+-----------+----------------------- | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | (9 rows) | ||
| + | |||
| + | postgres=# \c fueltracker | ||
| + | You are now connected to database " | ||
| + | fueltracker=# | ||
| + | List of schemas | ||
| + | Name | ||
| + | -------------+------------------- | ||
| + | | ||
| + | | ||
| + | | ||
| + | (3 rows) | ||
| + | |||
| + | fueltracker=# | ||
| + | List of relations | ||
| + | | ||
| + | -------------+--------------+-------+---------- | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | (8 rows) | ||
| + | |||
| + | fueltracker=# | ||
| + | List of relations | ||
| + | | ||
| + | -------------+--------------+-------+---------- | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | (9 rows) | ||
| + | |||
| + | </ | ||
| + | <code bash> | ||
| + | # 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 ===== | ===== DEV refresh ===== | ||
| ==== Backup fuelwatcher on PROD ==== | ==== Backup fuelwatcher on PROD ==== | ||
| - | < | + | < |
| - | docker exec -it FW_PostgreSQL | + | docker exec -it FT_PostgreSQL |
| cd / | cd / | ||
| - | pg_dump -d fuelwatcher > fuelwatcher_all_`date +%Y-%m-%dT%H-%M-%S`.backup | + | pg_dump -d fuelwatcher > fuelwatcher_RPi64_`date +%Y-%m-%dT%H-%M-%S`.backup |
| </ | </ | ||
| Login to root: | Login to root: | ||
| - | < | + | < |
| sudo su - | sudo su - | ||
| - | rsync -av / | + | rsync -av / |
| </ | </ | ||
| - | Copy backup to STONARD with MobaXterm | ||
| - | |||
| - | Copy backup to Container with beyond Compare (backups <--> RPi-WiFi backups) | ||
| ==== Restore on DEV ==== | ==== Restore on DEV ==== | ||
| + | Copy //daily// backup from PROD to DEV using Beyond Compare config **Docker/ | ||
| Login: | Login: | ||
| - | < | + | < |
| - | docker exec -it dev-container_devcontainer_db_1 | + | docker exec -it FT_PostgreSQL |
| su - postgres | su - postgres | ||
| + | |||
| + | psql | ||
| </ | </ | ||
| DROP and CREATE DATABASE | DROP and CREATE DATABASE | ||
| - | < | + | < |
| - | psql | + | |
| select pg_terminate_backend(pid) from pg_stat_activity where datname=' | select pg_terminate_backend(pid) from pg_stat_activity where datname=' | ||
| DROP DATABASE fuelwatcher; | DROP DATABASE fuelwatcher; | ||
| - | CREATE DATABASE | + | CREATE DATABASE |
| WITH | WITH | ||
| OWNER = postgres | OWNER = postgres | ||
| Line 53: | Line 114: | ||
| < | < | ||
| cd / | cd / | ||
| - | pg_dump -d fuelwatcher | + | pg_dump -d fueltracker |
| - | psql -d fuelwatcher < fuelwatcher_all_2021-08-01T05-23-25.backup | + | psql -d fuelwatcher < fueltracker_RPi64_2021-08-01T05-23-25.backup |
| </ | </ | ||
| TMP | TMP | ||
| - | < | + | < |
| pg_dumpall > dev-container_devcontainer_db_1_all_`date +%Y-%m-%dT%H-%M-%S`.backup | pg_dumpall > dev-container_devcontainer_db_1_all_`date +%Y-%m-%dT%H-%M-%S`.backup | ||
| - | cd /workspace/fuelwatcher/ | + | cd /workspace/FuelTracker/ |
| - | cd /workspace/fuelwatcher/ | + | cd /workspace/FuelTracker/ |
| + | </ | ||
| + | ===== Data Dictionary export ===== | ||
| + | For comparison between PROD and DEV. Perform on both: | ||
| + | <code bash> | ||
| + | pg_dump -Cs fueltracker > / | ||
| + | </ | ||
| + | ===== Version upgrade ===== | ||
| + | Test (gunzipped) restore of daily PROD backup to test PostgreSQL server (new version). | ||
| + | < | ||
| + | docker exec -it TEST_PostgreSQL /bin/bash | ||
| + | |||
| + | cd / | ||
| + | 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 ../ | ||
| + | </ | ||
| + | |||
| + | Move backups back to proper location | ||
| + | < | ||
| + | mv data_bak/ | ||
| + | </ | ||
| + | |||
| + | ===== Password fix ===== | ||
| + | <code sql> | ||
| + | SELECT | ||
| + | rolname, rolpassword ~ ' | ||
| + | FROM pg_authid | ||
| + | WHERE rolcanlogin; | ||
| + | </ | ||
| + | < | ||
| + | \password postgres | ||
| </ | </ | ||
| ===== References ===== | ===== References ===== | ||
| - | How to Upgrade Your PostgreSQL Passwords to SCRAM - https:// | + | [[https:// |
| + | |||
| + | [[https:// | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | [[https:// | ||
| + | [[https:// | ||