I'm developing a web map application that uses geoserver + express as its server and postgreSQL + postGIS as its database. In one part, I wanted to add some functionalities that users can upload their .shp files remotely. After spending days, I found that it's possible to execute shell commands just using exec function that both php and express provides for us. So because of psql and shp2pgsql are commands that can be executed in OS command line, exec function is a solution.
Although I use javascript at client and server, in the following I've given a simple piece of code for both php and express based on your need:
1) PHP
This is a part of your php code that your html form is submitted.
$username = 'your_username';
$password = 'your_password';
$dbname = 'target_databasename';
$tbname = 'target_tablename';
$srsname = 'reference_system';
$fileName = 'shapefile_name';
$directory = '\\directory\\' .$fileName. '.shp';
$shellCommand = "shp2pgsql -I -s " .$srsname. " " .$directory. " public." .$tbname. " | psql -U " .$username. " -d " .$dbname."";
$shapfileLoader = exec($shellCommand,$stdOut,$stdErr);
if (!$stdErr) {
echo $stdOut;
} else {
echo $stdErr;
}
2) Express
After defining a path in your express server, just add this js code:
var express = require('express');
var router = express.Router();
var { exec } = require('child_process');
router.post("/", function (_req, _res, _next) {
const postGISdb = 'db_name';
const user = 'your_username';
exec(`psql -U ${user} -d ${postGISdb}`, (err, stdOut, stdErr) => {
console.log(stdOut)
})
});
module.exports = router;
1) Don't forget to replace the database config information by your own.
2) You don't need QGIS anymore.
3) By using exec function, you should enter postgreSQL password so you need to create a .pgpass file to set password non-interactively and automatically. For more information, check this link.