I had the same problem. I had a bad import. It must be like this
import "github.com/jackc/pgx/v5/pgconn" var pgErr *pgconn.PgError if errors.As(err, &pgErr) { // code ... }