With ogr2ogr.
export CONN_STRING="host=localhost dbname=DATABASE user=USERNAME password=PASSWORD port=5432" # Import ogr2ogr -append -f PostgreSQL PG:dbname=DATABASE_NAME /path/to/your.gpx # Export ogr2ogr -f gpx -nlt MULTILINESTRING /path/to/output/tracks.gpx PG:"$CONN_STRING" "tracks(wkb_geometry)" ogr2ogr -f gpx -nlt MULTILINESTRING /path/to/output/routes.gpx PG:"$CONN_STRING" "routes(wkb_geometry)" ogr2ogr -f gpx -nlt POINT /path/to/output/waypoints.gpx PG:"$CONN_STRING" "waypoints(wkb_geometry)"
The wkb_geometry
references can be replaced with full SQL statements as required.