I'm loading an exported database with the below commands:
psql -c 'drop database database1'
psql -c 'create database database1'
psql database1 < script.sql
Now I'm trying to check if the final command succeeded:
if [[ $? -eq 0 ]]; then
echo "OK."
else
echo "Not OK."
fi
This always outputs "OK." the Exit code is always 0, even if script.sql completes with errors:
psql database1 < script.sql
ERROR: constraint "test_id" for relation "test" already exists
echo $?
0
How can I confirm the sql import succeeded?
psql -v "ON_ERROR_STOP=1" database1 < script.sqland test the results of that. You should get return code3. See the duplicate I flagged for more info.