add postgresql-tips

This commit is contained in:
Trent Palmer 2025-05-06 13:03:55 -07:00
parent 6be54ec220
commit 735c94254d
2 changed files with 62 additions and 0 deletions

View File

@ -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`

View File

@ -24,6 +24,7 @@ markdown_extensions:
nav: nav:
- Home: - Home:
- Home: index.md - Home: index.md
- "Postgresql Tips": posts/postgresql-tips.md
- "Open Elevation Api Server Upgrade": posts/open-elevation-api-server-upgrade.md - "Open Elevation Api Server Upgrade": posts/open-elevation-api-server-upgrade.md
- "Wireguard on Nspawn": posts/wireguard-on-nspawn.md - "Wireguard on Nspawn": posts/wireguard-on-nspawn.md
- "Sandbox IOT Network": posts/sandbox-iot-network.md - "Sandbox IOT Network": posts/sandbox-iot-network.md
@ -74,6 +75,7 @@ nav:
- FreeCodeCampChallenges: https://trentspalmer.github.io/fcc-challenges/ - FreeCodeCampChallenges: https://trentspalmer.github.io/fcc-challenges/
- DeviceLayout: https://trentpalmer.work/6a57bbe24d8244289610bf57533d6c6f/ - DeviceLayout: https://trentpalmer.work/6a57bbe24d8244289610bf57533d6c6f/
- Posts: - Posts:
- "Postgresql Tips": posts/postgresql-tips.md
- "Open Elevation Api Server Upgrade": posts/open-elevation-api-server-upgrade.md - "Open Elevation Api Server Upgrade": posts/open-elevation-api-server-upgrade.md
- "Wireguard on Nspawn": posts/wireguard-on-nspawn.md - "Wireguard on Nspawn": posts/wireguard-on-nspawn.md
- "Sandbox IOT Network": posts/sandbox-iot-network.md - "Sandbox IOT Network": posts/sandbox-iot-network.md