zipperer / migrate_database_with_michael

0 stars 0 forks source link

Meeting 20240130 #48

Closed zipperer closed 5 months ago

zipperer commented 5 months ago

Outline of steps

Run postgres in container (with volume and expose port)
- $ docker pull postgres
- $ docker run --name grace_and_truth_db_container --publish 5434:5432/tcp --env POSTGRES_PASSWORD=postgres --detach --volume grace_and_truth_db_container_volume_20240130:/var/lib/postgresql/data postgres:latest
- $ docker exec -it grace_and_truth_db_container
- > psql --username postgres
- psql> \l
- psql> \d
- psql> CREATE TABLE my_table (id SERIAL PRIMARY KEY, name VARCHAR);
- psql> INSERT INTO my_table (name) VALUES ('Alice'), ('Bob'), ('Charles');
- psql> SELECT * FROM my_table;
- psql> \q
- > exit

Connect to postgres in container via TablePlus
- make connection with user=postgres password=postgres port=5434 dbname=postgres

Observe that can stop the container and remove the container and restart the container with the previous volume and
the data is there
- $ docker stop grace_and_truth_db_container
- $ docker rm grace_and_truth_db_container
- $ docker ps -a
- $ docker images
- $ docker run --name grace_and_truth_db_container --publish 5434:5432/tcp --env POSTGRES_PASSWORD=postgres --detach --volume grace_and_truth_db_container_volume_20240130:/var/lib/postgresql/data postgres:latest
- $ docker exec -it grace_and_truth_db_container
- > psql --username postgres
- psql> \l
- psql> \d
- psql> SELECT * FROM my_table;
- psql> \q
- > exit

Observe that can restore contents from database currently running on host within container
[fill in details:
- on host, pg_dump command
- docker cp file into container
- restore to db in container
]
on host:
$ mkdir my_postgres_backups
$ pg_dump -Fc --username=postgres --dbname=postgres > my_postgres_backups/postgres_backup_${TIMESTAMP}_Fc_format.sql
$ docker container cp my_postgres_backups/postgres_backup_..._Fc_format.sql container-name:/my_postgres_backups
# ^ may need to first create the directory (/my_postgres_backups) in the container. could just copy into / for now

in container:
$ createdb --username=postgres -T template0 grace_and_truth_db_from_backup
$ pg_restore --username postgres -d grace_and_truth_db_from_backup /my_postgres_backups/postgres_backup_..._Fc_format.sql
$ psql --username postgres
psql> \connect grace_and_truth_db_from_backup

Observe that can do backups and move from container to host
[fill in details:
- in container, pg_dump command
- from host, docker cp the file onto the host
]
same process as above but do pg_dump in container, docker cp from container to host, and createdb and pg_restore on host

Andrew will look into lxc containers. See #47 .