Created
December 20, 2024 18:42
-
-
Save dianjuar/7028ea38dfe97ca2ddedeba7577caefe to your computer and use it in GitHub Desktop.
Dockerized postgesSQL local environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3' | |
services: | |
postgresql: | |
image: postgres:alpine | |
ports: | |
- 5435:5435 | |
volumes: | |
- ./database/db:/var/lib/postgresql/data | |
- ./database/db-restores:/tmp/restore | |
- ./database/init-scripts:/docker-entrypoint-initdb.d | |
environment: | |
- POSTGRES_USER=postgres | |
- POSTGRES_HOST_AUTH_METHOD=trust | |
- POSTGRES_DB=postgres | |
db-admin: | |
image: dpage/pgadmin4 | |
ports: | |
- 5050:5050 | |
links: | |
- postgresql | |
volumes: | |
- ./database/db-admin:/var/lib/pgadmin | |
environment: | |
- [email protected] | |
- PGADMIN_DEFAULT_PASSWORD=test1234 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
BACKUP_NAME=${1} | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <backup_name>" | |
echo "Please provide a name for the backup file" | |
exit 1 | |
fi | |
docker-compose exec postgresql /bin/bash -c "pg_dump \ | |
--host localhost \ | |
--username postgres \ | |
--format t \ | |
postgres > /tmp/restore/$BACKUP_NAME" | |
echo "Backup '$BACKUP_NAME' created successfully 🎉" | |
echo "Search for it under the folder 'database/db-restores'" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BACKUP_NAME=${1} | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <backup_file>" | |
echo "Please provide the backup file to restore" | |
exit 1 | |
fi | |
BACKUP_NAME_LOG="$BACKUP_NAME.log.txt" | |
echo 'Restoring ⚙️ ...' | |
docker-compose exec postgresql /bin/bash -c "pg_restore \ | |
--dbname postgres \ | |
--clean \ | |
--username postgres \ | |
--host 127.0.0.1 \ | |
/tmp/restore/$BACKUP_NAME 2> /tmp/restore/$BACKUP_NAME_LOG" | |
echo 'Restoration Done 🎉' | |
echo "Checkout for possible errors at database/db-restores/$BACKUP_NAME_LOG" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment