Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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?

share|improve this answer
    
I stopped it running as root and that fixed the problem right up for me. Thanks. –  Sam Marland Aug 14 '13 at 12:49
    
*I stopped it running as the postgres user. –  Sam Marland Aug 14 '13 at 13:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.