Debugging ogr2ogr "AddGeometryColumn failed" and "Terminating translation prematurely after failed translation of layer"

It appears to have been a different root cause (the above issue was resolved via permissions), but I ran into the same error when importing to PostGIS with ogr2ogr:

ERROR 1: Terminating translation prematurely after failed
translation of layer mybeautifulshapefile (use -skipfailures to
skip errors)

In my case it was a lingering shapefile (TIGER census places) precision bug, and the solution was fairly simple for anyone else who comes across it. Add this flag to ogr2ogr:

-lco PRECISION=NO 

AddGeometryColumn is a PostGIS function, so the error should be happening on the database... the source code for the client is in ogr/ogrsf_frmts/pg/ogrpgdatasource.cpp and it says (v1.11):

    osCommand.Printf(
             "SELECT AddGeometryColumn(%s,%s,%s,%d,'%s',%d)",
             pszEscapedSchemaNameSingleQuote, pszEscapedTableNameSingleQuote,
             OGRPGEscapeString(hPGConn, pszGFldName).c_str(),
             nSRSId, pszGeometryType, nDimension );

    hResult = OGRPG_PQexec(hPGConn, osCommand.c_str());

    if( !hResult
        || PQresultStatus(hResult) != PGRES_TUPLES_OK )
    {
        CPLError( CE_Failure, CPLE_AppDefined,
                  "AddGeometryColumn failed for layer %s, layer creation has failed.",
                  pszLayerName );

Obviously it would have been better if it had logged the exact command run (osCommand.c_str()), but oh well, that's just the client. You can still look at the database server logs for errors, and find e.g.:

ERROR:  permission denied for relation spatial_ref_sys
STATEMENT:  INSERT INTO spatial_ref_sys (srid,srtext,proj4text) VALUES ([...])

or:

ERROR:  AddGeometryColumn() - invalid SRID
CONTEXT:  SQL statement "SELECT AddGeometryColumn('',$1,$2,$3,$4,$5,$6,$7)"
    PL/pgSQL function addgeometrycolumn(character varying,character varying,character varying,integer,character varying,integer,boolean) line 5 at SQL statement
STATEMENT:  SELECT AddGeometryColumn([...])