How can I run command into container?

I have container with Postgres. I have to run one commnad “ALTER ROLE postgres SET search_path = constructor,public;
I can do next way:

docker exec - it id_container bash

then

psql -U postgres -d postgres

and then:

ALTER ROLE postgres SET search_path = constructor,public;

This script work.
But I want to use this script in one line, like this:

docker exec -it story_date_db bash | psql -U postgres -d postgres ALTER ROLE postgres SET search_path = ts_pfa2_constructor,public;

Unfortunately this code returns error:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = (unset),
	LC_CTYPE = "UTF-8",
	LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
psql: warning: extra command-line argument "ALTER" ignored
psql: warning: extra command-line argument "ROLE" ignored
psql: warning: extra command-line argument "postgres" ignored
psql: warning: extra command-line argument "SET" ignored
psql: warning: extra command-line argument "search_path" ignored
psql: warning: extra command-line argument "=" ignored
psql: warning: extra command-line argument "constructor,public" ignored
psql: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

This can’t work, as the commands after the pipe will be run on the host, outside the container.

Something like this should work though:

docker exec -i story_gate_db psql -U postgres -d postgres <<EOF
ALTER ROLE postgres SET search_path = ts_pfa2_constructor,public;
EOF

It redirects a here-doc string with the SQL statements to the command in the container. I wish I could cheat by looking into older pipeline scripts where I used this a lot in the past - but I can’t access them from my private laptop. I vaguely remember there was something special about -i and/or -t in this scenario, but I don’t remember wether they are required or actualy create a problem.

1 Like