1

To set up my project, I have to do quite a few commands and I'm trying to script this away. Some of these are in psql; so normally I'd go

psql -U postgres -h localhost -p 5433
(psql) create database test_database
(psql) \c test_database
(psql) \i integration-test/src/test/resources/init.sql 

The .init.sql contains stuff to fill the database with mock data.

In my bash script, I tried reducing this to

psql -U postgres -h localhost -p 5433 -c "create database test_database; \c test_database; \i integration-test/src/test/resources/init.sql"

However, this gets me

ERROR:  syntax error at or near "\"
LINE 1: create database test_database; \c fcs_analytics; \i integrat...
                                   ^

How do I execute these commands properly from my script?

1 Answer 1

3

Have you tried ?

psql -U postgres -h localhost -p 5433 << 'EOF'
create database test_database
\c test_database
\i integration-test/src/test/resources/init.sql
EOF
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that! So appending multiline commands as a string is simply a no-go?
Appending multiline commands as a string can still work, but dealing with quotes and backblashes is too much hassle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.