From PostGis to Shapefile using pgsql2shp (Linux Command Line Version)

For this post, we will use the same setup as in Pandas to PostGIS using Psycopg2. The database connection parameters are the following

"host"      : "localhost",
"database"  : "globaldata",
"user"      : "myuser",
"password"  : "Passw0rd"

And inside the globaldata database, there is a stations table that you can query to get the following result (using PgAdmin for instance).

Now here is how you can get a shapefile from this data using pgsql2shp.

# First create a local directory to store your shapefile files
mkdir my_shapefiles
cd my_shapefiles 

# Notice the correspondance between every parameter and the connection parameters above
pgsql2shp -f stations.shp -h localhost -u myuser -P Passw0rd  stations "select * from stations;"

If your command is successful, it should produce an output similar to mine below

Initializing... 
Done (postgis major version: 3).
Output shape: Point
Dumping: X [100 rows].

Your files should now be ready

>>> ls
stations.cpg  stations.dbf  stations.prj  stations.shp  stations.shx

You can open your shapefile with any GIS client, like with QGIS for instance.