I'm importing data into postgresql with shp2psql and pyhton's subprocess module.

command = 'shp2pgsql -s {SRID} -a -g geom -W "UTF-8" {SHP_path} {table_name} -I | ' \
          'psql -h {hostname} -p {port} -d {db_name} -U {username}'\
    .format(SRID=4326, SHP_path=shp_file, table_name=table_name, 
            hostname=hostname, port=port, db_name=db_name, username=username)

subprocess.call(command, shell=True)

The problem is that I cannot intercept import errors like

current transaction is aborted, commands ignored until end of transaction block

They are happening internally and don't stop the subprocess from execution so I have no means to control the result of execution. Is there any way to raise such errors in the main code?


I suppose you use Python 2.7. You can change to check_call method of subprocess and you can redirect stdin, stdout and stderr, see: https://docs.python.org/2.7/library/subprocess.html Using Python 3.5+ you have more control.

  • My Pyhton version is 3.4.2 – Basile May 24 '18 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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