make and save outputUse a Make target to:
psql inside a Docker container.sql file from the hostThis target is fine for interactive use:
psql:
docker exec -it $(DB_CONTAINER) psql -U $(DB_USER) -d $(DB_NAME)
But it fails for file input or output redirection because:
-t asks Docker to allocate a TTYthe input device is not a TTYAlso, psql -f some/path.sql looks for the file
inside the container, not on the host.
Use two targets:
psql:
docker exec -it $(DB_CONTAINER) psql -U $(DB_USER) -d $(DB_NAME)
psql-file:
docker exec -i $(DB_CONTAINER) psql -U $(DB_USER) -d $(DB_NAME)
make psqlcat backend/db/vendor.sql | make psql-file > ~/etc/tmp/output.txtcat backend/db/vendor.sql reads the file on the
hostpsql through standard inputdocker exec -i keeps standard input open without
allocating a TTY> writes the query output to a host file-it for interactive shells-i for pipes and redirectionIf you want a reusable target for container-side file paths:
psql-args:
docker exec -i $(DB_CONTAINER) psql -U $(DB_USER) -d $(DB_NAME) $(ARGS)
Example:
make psql-args ARGS="-f /app/backend/db/vendor.sql"That only works if the file path exists inside the container.
For reliability, prefer the pipe method:
cat backend/db/vendor.sql | make psql-file > ~/etc/tmp/output.txt