Index: ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp
===================================================================
--- ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp	(revision 22616)
+++ ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp	(working copy)
@@ -1133,37 +1133,58 @@
     else if( bHasPostGISGeometry || bHasPostGISGeography )
     {
         osCommand = osCommand + OGRPGEscapeColumnName(pszGeomColumn) + " = ";
-        char    *pszWKT = NULL;
-
+        OGRGeometry *poGeom = NULL;
+        
         if( poFeature->GetGeometryRef() != NULL )
         {
-            OGRGeometry *poGeom = (OGRGeometry *) poFeature->GetGeometryRef();
+            poGeom = (OGRGeometry *) poFeature->GetGeometryRef();
 
             poGeom->closeRings();
             poGeom->setCoordinateDimension( nCoordDimension );
 
-            poGeom->exportToWkt( &pszWKT );
         }
 
-        if( pszWKT != NULL )
+        if ( !CSLTestBoolean(CPLGetConfigOption("PG_USE_TEXT", "NO")) )
         {
-            if( bHasPostGISGeography )
-                osCommand +=
-                    CPLString().Printf(
-                        "ST_GeographyFromText('SRID=%d;%s'::TEXT) ", nSRSId, pszWKT );
-            else if( poDS->sPostGISVersion.nMajor >= 1 )
-                osCommand +=
-                    CPLString().Printf(
-                        "GeomFromEWKT('SRID=%d;%s'::TEXT) ", nSRSId, pszWKT );
+            char    *pszHexEWKB = NULL;
+            pszHexEWKB = GeometryToHex( poGeom, nSRSId );
+            if ( pszHexEWKB != NULL )
+            {
+                if ( bHasPostGISGeography )
+                    osCommand += CPLString().Printf("'%s'::GEOGRAPHY", pszHexEWKB);
+                else
+                    osCommand += CPLString().Printf("'%s'::GEOMETRY", pszHexEWKB);
+                OGRFree( pszHexEWKB );
+            }
             else
-                osCommand += 
-                    CPLString().Printf(
-                        "GeometryFromText('%s'::TEXT,%d) ", pszWKT, nSRSId );
-            OGRFree( pszWKT );
+                osCommand += "NULL";    
         }
         else
-            osCommand += "NULL";
+        {
+            char    *pszWKT = NULL;
+    
+            poGeom->exportToWkt( &pszWKT );
 
+            if( pszWKT != NULL )
+            {
+                if( bHasPostGISGeography )
+                    osCommand +=
+                        CPLString().Printf(
+                            "ST_GeographyFromText('SRID=%d;%s'::TEXT) ", nSRSId, pszWKT );
+                else if( poDS->sPostGISVersion.nMajor >= 1 )
+                    osCommand +=
+                        CPLString().Printf(
+                            "GeomFromEWKT('SRID=%d;%s'::TEXT) ", nSRSId, pszWKT );
+                else
+                    osCommand += 
+                        CPLString().Printf(
+                            "GeometryFromText('%s'::TEXT,%d) ", pszWKT, nSRSId );
+                OGRFree( pszWKT );
+            }
+            else
+                osCommand += "NULL";
+
+        }
         bNeedComma = TRUE;
     }
 
@@ -1460,34 +1481,53 @@
     bNeedComma = FALSE;
     if( (bHasPostGISGeometry || bHasPostGISGeography) && poGeom != NULL)
     {
-        char    *pszWKT = NULL;
         
         CheckGeomTypeCompatibility(poGeom);
 
         poGeom->closeRings();
         poGeom->setCoordinateDimension( nCoordDimension );
 
-        poGeom->exportToWkt( &pszWKT );
 
-        if( pszWKT != NULL )
+        if ( !CSLTestBoolean(CPLGetConfigOption("PG_USE_TEXT", "NO")) )
         {
-            if( bHasPostGISGeography )
-                osCommand +=
-                    CPLString().Printf(
-                        "ST_GeographyFromText('SRID=%d;%s'::TEXT) ", nSRSId, pszWKT );
-            else if( poDS->sPostGISVersion.nMajor >= 1 )
-                osCommand +=
-                    CPLString().Printf(
-                        "GeomFromEWKT('SRID=%d;%s'::TEXT) ", nSRSId, pszWKT );
+            char    *pszHexEWKB = NULL;
+            pszHexEWKB = GeometryToHex( poGeom, nSRSId );
+            if ( pszHexEWKB != NULL )
+            {
+                if ( bHasPostGISGeography )
+                    osCommand += CPLString().Printf("'%s'::GEOGRAPHY", pszHexEWKB);
+                else
+                    osCommand += CPLString().Printf("'%s'::GEOMETRY", pszHexEWKB);
+                OGRFree( pszHexEWKB );
+            }
             else
-                osCommand += 
-                    CPLString().Printf(
-                        "GeometryFromText('%s'::TEXT,%d) ", pszWKT, nSRSId );
-            OGRFree( pszWKT );
+                osCommand += "NULL";
         }
         else
-            osCommand += "''";
+        { 
+            char    *pszWKT = NULL;
+            poGeom->exportToWkt( &pszWKT );
+
+            if( pszWKT != NULL )
+            {
+                if( bHasPostGISGeography )
+                    osCommand +=
+                        CPLString().Printf(
+                            "ST_GeographyFromText('SRID=%d;%s'::TEXT) ", nSRSId, pszWKT );
+                else if( poDS->sPostGISVersion.nMajor >= 1 )
+                    osCommand +=
+                        CPLString().Printf(
+                            "GeomFromEWKT('SRID=%d;%s'::TEXT) ", nSRSId, pszWKT );
+                else
+                    osCommand += 
+                        CPLString().Printf(
+                            "GeometryFromText('%s'::TEXT,%d) ", pszWKT, nSRSId );
+                OGRFree( pszWKT );
+            }
+            else
+                osCommand += "''";
             
+        }
         bNeedComma = TRUE;
     }
     else if( bHasWkb && !bWkbAsOid && poGeom != NULL )
