0

I am running a postgresql in docker container and trying to insert some sample data into this database.

prepare_source_data.sql exists in the host environment.

here I tried

docker exec -it postgres bash psql "postgres://postgres:postgres@localhost/test"  -f ./prepare_source_data.sql

but got some errors

/usr/bin/psql: line 19: use: command not found
/usr/bin/psql: line 20: use: command not found
/usr/bin/psql: line 21: use: command not found
/usr/bin/psql: line 22: use: command not found
/usr/bin/psql: psql: line 24: syntax error near unexpected token `$version,'
/usr/bin/psql: psql: line 24: `my ($version, $cluster);'

However executing the commands work well

  docker exec -it postgres bash and then psql -U postgres 

any idea how would I insert the data into the container in the host environment

3
  • The error message, syntax error near unexpected token ``$version,' is pretty clear. psql is not accepting $version, which is understandable as there is nothing here psql that says it does. The closest you get is $1, etc with bind. You will need to show what is in prepare_source_data.sql and also give some explanation of what you expect the script to do. Commented Nov 25 at 6:12
  • 1
    The SQL script that you are trying to run is not SQL - or some other SQL dialect. You'll have to rewrite it to PostgreSQL's SQL syntax. Commented Nov 25 at 7:33
  • You shouldn't need the docker exec debugging tool at all for this. Use the psql client directly from your host system, targeting the first docker run -p or Compose ports: number you used when you started the database container. This also saves you the trouble of copying the file into the container. Commented Nov 25 at 11:02

1 Answer 1

1

docker exec -it postgres bash psql "postgres://postgres:postgres@localhost/test" -f ./prepare_source_data.sql

This execute inside the bash.everything after the bash is passed as arguement to bash not a command so bash get postgres://postgres:postgres@localhost/test -f ./prepare_source_data.sql

This may cause the syntax error

docker exec -i postgres psql "postgres://postgres:postgres@localhost/test" < ./prepare_source_data.sql

This execute inside the psql

docker exec -it --The docker exec command runs a new command in a running container.'<' is a shell redirection operator this will tells to take the contents of the file

if the sql file is inside the container you can use

psql -U postgres -f /prepare_source_data.sql

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.