Postgres 迁移记录

SimpleMaxR Lv2

环境

运行在本地的 Postgres 容器,名称为 lobe-postgres,用户名是 postgres

命令

列出所有数据库

1
docker exec lobe-postgres psql -U postgres -c "\l"

列出数据库中的所有表

1
2
3
4
5
# 进入容器
docker exec -it lobe-postgres psql -U postgres -d your_database

## 列出数据表
\dt

使用 pg_dump 导出数据库

1
docker exec lobe-postgres pg_dump -U your_username -d your_database > backup.sql

将 backup 文件复制到目标容器

1
docker cp backup.sql target_postgres_container:/backup.sql

创建新数据库并导入数据

1
2
3
docker exec -it target_postgres_container psql -U postgres -c "CREATE DATABASE new_database;"

docker exec -it target_postgres_container psql -U postgres -d new_database -f /backup.sql
  • Title: Postgres 迁移记录
  • Author: SimpleMaxR
  • Created at : 2024-10-02 10:57:26
  • Updated at : 2025-02-11 12:15:45
  • Link: https://www.hgzre.tech/2024/10/02/Postgres-迁移记录/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
Postgres 迁移记录