I want to insert a shapefile into a PostGIS database using PHP. I go through the examples and I tried the following:

$command = 'shp2pgsql -s 2100 -d /D:\CHECK DATA-group\2016\Ayomi\JULY\12FCP151Sup88Sh99\121800151s00088s0099\CM121800151s00088s0099Poly.shp | psql -h localhost -p 5432 -d ssh -U postgres';

But it is not working. Then in the forum, I saw an instruction to add the exec($command,$out,$ret); to the above code. Can any one explain me what is meant by $out,$ret variables?

  • no i don't get any errors?even after i add "exec($command,$out,$ret);" it doesn't generate any errors.but shape file has not tranfer to the data base Nov 8, 2018 at 7:29
  • 1) this is run on the server, so make sure that the shp is indeed on the server (not your local D drive). 2) there is a space in your path, which is likely to break the command. Try adding double quotes around the path
    – JGH
    Nov 8, 2018 at 12:55
  • I am new to this type of scripts.Actually,this is in my dektop.i have installed wamp and postgres sql in my desktop.What my plan was try this in my desktop first and then use it in the server with changes.According to my current environment, should I change the above script totally? Nov 8, 2018 at 15:10
  • can you please tell me where is the space exits.You meant space between equal (= ) sign and 'shp2pgsql? Nov 12, 2018 at 6:00
  • He meant the space in the file path near CHECK DATA. Try using $command = 'shp2pgsql -s 2100 -d "D:\CHECK DATA-group\2016\Ayomi\JULY\12FCP151Sup88Sh99\121800151s00088s0099\CM121800151s00088s0099Poly.shp" | psql -h localhost -p 5432 -d ssh -U postgres'; If password is not the issue, this should be it. Nov 12, 2018 at 6:11

1 Answer 1


psql requires password to be entered which you can supply by creating a .pgpass file.

or by setting the PGPASSWORD environment variable (Not recommended as per docs)

Use of this environment variable is not recommended for security reasons, as some operating systems allow non-root users to see process environment variables via ps; instead consider using a password file

But for a quick check you can try adding


in your php code before the command.

From PHP Docs


If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().


If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.

check the $out and $ret variables for the output and status of the executed command.

  • When i insert data to the postgis database(ssh) manually using the shp2sql tool it automatically created a table in the data base.So should i first create a empty table here before run this code? Nov 8, 2018 at 10:08
  • Sorry, table is not the issue. I forgot that it is generated automatically. How are you passing the password for psql? Nov 8, 2018 at 11:03
  • Actually ,I don't have a correct idea.Normally my ssh database has a password.If it is so,how should I add it? Nov 8, 2018 at 14:59
  • For a quick check try adding putenv('PGPASSWORD=password'); getenv('PGPASSWORD'); in your php code before the command. or shp2pgsql -s 2100 -d /D:\CHECK DATA-group\2016\Ayomi\JULY\12FCP151Sup88Sh99\121800151s00088s0099\CM121800151s00088s0099Poly.shp | PGPASSWORD=password psql -h localhost -p 5432 -d ssh -U postgres For more info on setting PGPASSWORD Check this answer Nov 9, 2018 at 7:31
  • I tried all these 3 methods.Still no positive results Nov 12, 2018 at 4:36

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.