From 735c94254db26393bd4bfc1b5ac2677bda8383c0 Mon Sep 17 00:00:00 2001 From: Trent Palmer Date: Tue, 6 May 2025 13:03:55 -0700 Subject: [PATCH] add postgresql-tips --- docs/posts/postgresql-tips.md | 60 +++++++++++++++++++++++++++++++++++ mkdocs.yml | 2 ++ 2 files changed, 62 insertions(+) create mode 100644 docs/posts/postgresql-tips.md diff --git a/docs/posts/postgresql-tips.md b/docs/posts/postgresql-tips.md new file mode 100644 index 0000000..8960706 --- /dev/null +++ b/docs/posts/postgresql-tips.md @@ -0,0 +1,60 @@ +--- +title: "Postgresql Tips" +date: 2025-04-30 +draft: false +tags: ["linux", "Debian", "Postgresql", "Database", "pg_dump"] +authors: ["trent"] +post: 37 +--- +date: 2025-04-30 + +## Introduction +Sometimes you want to recreate an postgresql database on a different +server or postgresql instance. +And so here are some tips how to do that. + + +## Backup and Restore Using Tar File +### create a backup with `pg_dump` +* switch to postgres user `su - postgres` +* create backup `pg_dump -d mytestdb -F tar -f mytestdb.tar` +* or with specific port `pg_dump -p5432 -d mytestdb -F tar -f mytestdb.tar` +* examine contents of tar `tar -tvf mytestdb.tar` +* alternately make a compressed backup +* `pg_dump -d mytestdb -Fc -f mytestdb_compressed_backup` + +### restore with `pg_restore` +* switch to postgres user `su - postgres` +* start psql `psql -p5432` +* `postgres# CREATE USER mytestuser;` +* `postgres# \password mytestuser` +* `postgres# CREATE DATABASE mytestdb WITH OWNER mytestuser;` +* list users `postgres# \du` +* list databases `postgres# \l` +* `exit` +* `pg_restore -p5432 -d mytestdb mytestdb.tar` +* or `pg_restore -p5432 -d mytestdb mytestdb_compressed_backup` + +### verify the restored database +* switch to postgres user `su - postgres` +* start psql `psql -p5432` +* change database `postgres# \c mytestdb` +* list tables `postgres# \d` or `postgres# \dt` + +## Backup and Restore Schema Only +### create a backup with `pg_dump` +* switch to postgres user `su - postgres` +* create backup `pg_dump --schema-only -d mytestdb -f mytestdb_schema.sql` +* or with specific port `pg_dump -p54342 --schema-only -d mytestdb -f mytestdb_schema.sql` +* examine sql file `less mytestdb_schema.sql` +* note that the generated sql assigns ownership of each table, to that of the original database owner, upon creation + +### restore with `psql` command +* switch to postgres user `su - postgres` +* `psql -p5432 -d mytestdb -f mytestdb_schema.sql` + +### verify the restored database +* switch to postgres user `su - postgres` +* start psql `psql -p5432` +* change database `postgres# \c mytestdb` +* list tables `postgres# \d` or `postgres# \dt` diff --git a/mkdocs.yml b/mkdocs.yml index a2860b8..56c587e 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -24,6 +24,7 @@ markdown_extensions: nav: - Home: - Home: index.md + - "Postgresql Tips": posts/postgresql-tips.md - "Open Elevation Api Server Upgrade": posts/open-elevation-api-server-upgrade.md - "Wireguard on Nspawn": posts/wireguard-on-nspawn.md - "Sandbox IOT Network": posts/sandbox-iot-network.md @@ -74,6 +75,7 @@ nav: - FreeCodeCampChallenges: https://trentspalmer.github.io/fcc-challenges/ - DeviceLayout: https://trentpalmer.work/6a57bbe24d8244289610bf57533d6c6f/ - Posts: + - "Postgresql Tips": posts/postgresql-tips.md - "Open Elevation Api Server Upgrade": posts/open-elevation-api-server-upgrade.md - "Wireguard on Nspawn": posts/wireguard-on-nspawn.md - "Sandbox IOT Network": posts/sandbox-iot-network.md