Skip to content

Instantly share code, notes, and snippets.

@zseta
Created January 2, 2024 20:07
Show Gist options
  • Save zseta/24bfbfcd16e5e47579f536f8d78c7a6c to your computer and use it in GitHub Desktop.
Save zseta/24bfbfcd16e5e47579f536f8d78c7a6c to your computer and use it in GitHub Desktop.
Backup Docker PostgreSQL database using SSH connection

Backup process with vanilla PostgreSQL or TimescaleDB

Connect to remote instance using SSH:

ssh example.com

List running Docker containers:

docker ps

Start the bash inside the container:

docker exec -it example-container bash

Create backup file:

pg_dump postgres > backup.sql --user postgres

If you're using TimescaleDB:

pg_dump \
--format=plain \
--quote-all-identifiers \
--no-tablespaces \
--no-owner \
--no-privileges  \
--file=dump.sql \
--user postgres

Copy backup file from container to host:

docker cp example-container:/backup.sql ~/

Copy file from host to local computer through SSH connection:

cp root@example.com:~/backup.sql ~/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment