运行容器

Ref: https://betterprogramming.pub/connect-from-local-machine-to-postgresql-docker-container-f785f00461a7

  1. docker run -d -p 5432:5432 --name my-postgres -e POSTGRES_PASSWORD=root postgres
  2. # 进入容器 inside the container
  3. docker exec -it my-postgres bash
  4. # 连接数据库
  5. psql -U postgres
  6. # 连接远程数据库
  7. psql -h localhost -p 5432 -U postgres -W

备份数据库

Ref: https://stackoverflow.com/a/29913462/8945448

  1. # backup SQL
  2. docker exec -t <container_id> pg_dumpall -c -U postgres > dump_`date +%d-%m-%Y"_"%H_%M_%S`.sql
  3. # restore SQL
  4. cat <your_dump>.sql | docker exec -i <container_id> psql -U postgres