Home > Back-end >  How do I add '\c' to a psql command executed from a bash script?
How do I add '\c' to a psql command executed from a bash script?

Time:10-04

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?

CodePudding user response:

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
  • Related