主页

docker postgres 导入一个表的数据

2025-03-08 08:13PM

1. 查看 docker 中运行的 postgres

# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS       PORTS      NAMES
45xxxxxx8d123   postgres   "docker-entrypoint.s…"   14 months ago   Up 9 hours   5432/tcp   objective_jang

2. 将本地 /tmp/ 文件夹下面的  文件复制到 Docker 容器中

$ sudo docker cp /tmp/industry_materials_2025_03_08.sql 45xxxxxx8d123:/tmp/

3. 进入 postgres 容器

$ docker exec -it my_postgres psql -U postgres -d your_database

4. 在容器内部使用 psql 命令连接 PostgreSQL 数据库

$ psql -U postgres

5. 可以直接导入数据,我这里是删除,重新创建表

$ DROP TABLE IF EXISTS industry_materials;

$ CREATE TABLE industry_materials ( id SERIAL PRIMARY KEY, material_id INT NOT NULL, industry_id INT NOT NULL, created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW() );

6. 导入数据

$ \i /tmp/industry_materials_2025_03_08.sql 

这样数据就可以成功导入啦! 

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论