0

I need to write a script that imports a DB schema in to PostgreSQL database which will run as part of a larger build script. I run the following script as root.

#Change ownership of copied Schema
chown postgres:postgres /var/lib/pgsql/ddl.sql

su - postgres

cd ~

psql -Ubuild test < /var/lib/pgsql/ddl.sql

exit

The problem I have is, to actually allow the import to work I have to type exit after the script has finished executing. I have tried adding an additional exit at the end of the script but it doesn't seem to make a difference.

Any ideas would be great

1 Answer 1

1

Maybe try to place it on the background with &:

#!/bin/bash
chown postgres:postgres /var/lib/pgsql/ddl.sql
su - postgres
cd ~
psql -Ubuild test < /var/lib/pgsql/ddl.sql &

Make sure you run it in a script probably to prevent job control.

If it pauses during process try to adding more configurations:

set +o monitor
psql -Ubuild test < /var/lib/pgsql/ddl.sql &
disown

And are you sure you still need to su to postgres when you already specify a different user with -Ubuild?

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

2 Comments

I stopped it running as root and that fixed the problem right up for me. Thanks.
*I stopped it running as the postgres user.

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.