Index: ogr/ogrsf_frmts/pg/ogrpgdatasource.cpp
===================================================================
--- ogr/ogrsf_frmts/pg/ogrpgdatasource.cpp	(revision 13522)
+++ ogr/ogrsf_frmts/pg/ogrpgdatasource.cpp	(working copy)
@@ -945,11 +945,54 @@
     char                szCommand[10000];
     char                *pszWKT = NULL;
     int                 nSRSId = -1;
+    const char         *pszAuthorityName;
 
     if( poSRS == NULL )
         return -1;
 
+    pszAuthorityName = poSRS->GetAuthorityName(NULL);
+    
 /* -------------------------------------------------------------------- */
+/*      Check whether the EPSG authority code is already mapped to a    */
+/*      SRS ID.                                                         */
+/* -------------------------------------------------------------------- */
+    if( EQUAL( pszAuthorityName, "EPSG" ) )
+    {
+        int             nAuthorityCode;
+        
+        /* For the root authority name 'EPSG', the authority code
+         * should always be integral
+         */
+        nAuthorityCode = atoi( poSRS->GetAuthorityCode(NULL) );
+        
+        sprintf( szCommand, "SELECT srid FROM spatial_ref_sys WHERE "
+                            "auth_name = '%s' AND auth_srid = %d",
+                            poSRS->GetAuthorityName(NULL),
+                            nAuthorityCode );
+        
+        hResult = PQexec(hPGConn, "BEGIN");
+        OGRPGClearResult( hResult );
+        
+        hResult = PQexec(hPGConn, szCommand);
+        
+        if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK
+            && PQntuples(hResult) > 0 )
+        {
+            nSRSId = atoi(PQgetvalue( hResult, 0, 0 ));
+            
+            OGRPGClearResult( hResult );
+            
+            hResult = PQexec(hPGConn, "COMMIT");
+            OGRPGClearResult( hResult );
+            
+            return nSRSId;
+        }
+        
+        hResult = PQexec(hPGConn, "COMMIT");
+        OGRPGClearResult( hResult );
+    }
+    
+/* -------------------------------------------------------------------- */
 /*      Translate SRS to WKT.                                           */
 /* -------------------------------------------------------------------- */
     if( poSRS->exportToWkt( &pszWKT ) != OGRERR_NONE )
@@ -1041,11 +1084,26 @@
 		
         return -1;
     }
+    
+    if( EQUAL(pszAuthorityName, "EPSG") )
+    {
+        int             nAuthorityCode;
+        
+        nAuthorityCode = atoi( poSRS->GetAuthorityCode(NULL) );
+        
+        sprintf( szCommand,
+                 "INSERT INTO spatial_ref_sys (srid,srtext,proj4text,auth_name,auth_srid) "
+                 "VALUES (%d, '%s', '%s', '%s', %d)",
+                 nSRSId, pszWKT, pszProj4, pszAuthorityName,
+                 nAuthorityCode );
+    }
+    else
+    {
+        sprintf( szCommand,
+                 "INSERT INTO spatial_ref_sys (srid,srtext,proj4text) VALUES (%d,'%s','%s')",
+                 nSRSId, pszWKT, pszProj4 );
+    }
 
-    sprintf( szCommand,
-             "INSERT INTO spatial_ref_sys (srid,srtext,proj4text) VALUES (%d,'%s','%s')",
-             nSRSId, pszWKT, pszProj4 );
-	
     // Free everything that was allocated.
     CPLFree( pszProj4 );
     CPLFree( pszWKT);

