如何编写一个 bash 脚本来访问 docker 中的数据库然后插入数据?
How to write a bash script that access to database inside docker then insert data?
假设我要经过
的路线
进入docker
进入postgreSQL数据库
创建table
插入数据
因为我正在测试,所以经常这样做很麻烦。我想编写一个 bash 脚本(.sh 文件)来完成 1 运行.
中的所有操作
这是我需要的所有命令行 运行
docker exec -it <mycontainer> bash
psql -h localhost -p 5432 -U postgres
CREATE SCHEMA bank;
CREATE TABLE bank.holding (
holding_id int,
user_id int,
holding_stock varchar(8),
holding_quantity int,
datetime_created timestamp,
datetime_updated timestamp,
primary key(holding_id)
);
insert into bank.holding values (102100, 2, 'VFIAX', 10, now(), now());
首先,如果您公开您的集装箱港口
,它将帮助您实现流程自动化
docker container run ... -p 5432:5432 ...
上述过程将使您能够通过直接访问您的 localhost:5432
来访问容器端口
下一个运行
psql -h localhost -p 5432 -U postgres -f bash_automation_script.txt
bash_automation_script.txt
CREATE SCHEMA bank;
CREATE TABLE bank.holding (
holding_id int,
user_id int,
holding_stock varchar(8),
holding_quantity int,
datetime_created timestamp,
datetime_updated timestamp,
primary key(holding_id)
);
insert into bank.holding values (102100, 2, 'VFIAX', 10, now(), now());
假设我要经过
的路线进入docker
进入postgreSQL数据库
创建table
插入数据
因为我正在测试,所以经常这样做很麻烦。我想编写一个 bash 脚本(.sh 文件)来完成 1 运行.
中的所有操作这是我需要的所有命令行 运行
docker exec -it <mycontainer> bash
psql -h localhost -p 5432 -U postgres
CREATE SCHEMA bank;
CREATE TABLE bank.holding (
holding_id int,
user_id int,
holding_stock varchar(8),
holding_quantity int,
datetime_created timestamp,
datetime_updated timestamp,
primary key(holding_id)
);
insert into bank.holding values (102100, 2, 'VFIAX', 10, now(), now());
首先,如果您公开您的集装箱港口
,它将帮助您实现流程自动化docker container run ... -p 5432:5432 ...
上述过程将使您能够通过直接访问您的 localhost:5432
下一个运行
psql -h localhost -p 5432 -U postgres -f bash_automation_script.txt
bash_automation_script.txt
CREATE SCHEMA bank;
CREATE TABLE bank.holding (
holding_id int,
user_id int,
holding_stock varchar(8),
holding_quantity int,
datetime_created timestamp,
datetime_updated timestamp,
primary key(holding_id)
);
insert into bank.holding values (102100, 2, 'VFIAX', 10, now(), now());