How to runnig sql script file

hello,
I want .sql file run with exec command.
image: postgres:alpine
connection : succesful

#docker cp ./test.sql postgresql_database/docker-entrypoint-initdb.d/test.sql
# docker exec postgresql_database -u postgres psql postgres -f docker-entrypoint-initdb.d/test.sql 
OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: \"-u\": executable file not found in $PATH": unknown

how does this work? thank you for help

@erkant87 did you find the solution for this? I am now having the same issue.

Thanks!

Hi.

Example

Start a PostgreSQL container

🐳  gforghetti:[~/Downloads] $ docker container run -it --detach --name my_db --env POSTGRES_DB=MY_DB --env POSTGRES_USER=my_user --env POSTGRES_PASSWORD=secret postgres:latest
699f1eea11c379b35aa82599522e0053128fbfa41fb72f64dbc00d08cfd5e06e

List the container

🐳  gforghetti:[~/Downloads] $ docker container ls --filter name=my_db
CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS                         PORTS               NAMES
699f1eea11c3        postgres:latest                       "docker-entrypoint.s…"   11 seconds ago      Up 10 seconds                  5432/tcp            my_db

Create a file with an sql command

🐳  gforghetti:[~/Downloads] $ echo 'select version();' > test.sql

Copy the sql fle to the container

🐳  gforghetti:[~/Downloads] $ docker container cp test.sql my_db:/

Run the command in the sql file

🐳  gforghetti:[~/Downloads] $ docker container exec -it my_db psql --dbname=MY_DB --username my_user -f /test.sql
                                                             version

--------------------------------------------------------------------------------
--------------------------------------------------
 PostgreSQL 11.2 (Debian 11.2-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gc
c (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit
(1 row)

🐳  gforghetti:[~/Downloads] $
1 Like

Hi! Thank you so much for your response. I’m sure I am doing something silly. But I’m still having issues executing the file.

To copy the file over I did: docker container cp "C:\ClientPortal_Create.sql" <container_id>:/

So i see the file in the location that I copied it over to: image

Then to exec it: docker exec -it <container_id> /ClientPortal_Create.sql --dbname=<db_name> --username sa -f /ClientPortal_Create.sql

However I still get: OCI runtime exec failed: exec failed: container_linux.go:344: starting container process caused "exec format error": unknown

The sql script is created from vs 2017 from a sql database project. The script when executed in ssms and in SQLCMD mode it executes just fine.

Is there something else I am missing when doing this? I appreciate all the help!

Looks like “something is wrong” with the /ClientPortal_Create.sql script.
I don’t know what it looks like inside as you have not shown it’s contents.
I recommend you bring up a shell prompt inside the container and run it manually first to debug it.
You are also copying the /ClientPortal_Create.sql script with docker cp command.
Not sure if that file has the necessary “execute” permissions for linux if you are copying it from a Windows client.

You should build a Docker Image with the sql command file built it (Dockerfile COPY) and then run a docker container from the image.

Notice in my example I ran the psql client command.