Sam Marland
Sam Marland

Reputation: 572

Running Script to import database schema in to PostgreSQL via Bash

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

Upvotes: 0

Views: 2173

Answers (1)

konsolebox
konsolebox

Reputation: 75458

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?

Upvotes: 1

Related Questions