# Class PostgresException

Namespace
  Npgsql

Assembly
  Npgsql.dll

The exception that is thrown when the PostgreSQL backend reports errors (e.g. query SQL issues, constraint violations).

    [Serializable]
    public sealed class PostgresException : NpgsqlException, ISerializable

Inheritance
  object
  Exception
  SystemException
  ExternalException
  DbException
  NpgsqlException
  PostgresException

Implements
  ISerializable

Inherited Members
  NpgsqlException.BatchCommand
  ExternalException.ErrorCode
  Exception.GetBaseException()
  Exception.GetType()
  Exception.Data
  Exception.HelpLink
  Exception.HResult
  Exception.InnerException
  Exception.Message
  Exception.Source
  Exception.StackTrace
  Exception.TargetSite
  object.Equals(object)
  object.Equals(object, object)
  object.GetHashCode()
  object.ReferenceEquals(object, object)

## Remarks

This exception only corresponds to a PostgreSQL-delivered error. Other errors (e.g. network issues) will be raised via NpgsqlException, and purely Npgsql-related issues which aren't related to the server will be raised via the standard CLR exceptions (e.g. ArgumentException).

See https://www.postgresql.org/docs/current/static/errcodes-appendix.html, https://www.postgresql.org/docs/current/static/protocol-error-fields.html

## Constructors

### PostgresException(string, string, string, string)

Creates a new instance.

    public PostgresException(string messageText, string severity, string invariantSeverity, string sqlState)

#### Parameters

`messageText` string
`severity` string
`invariantSeverity` string
`sqlState` string

### PostgresException(string, string, string, string, string?, string?, int, int, string?, string?, string?, string?, string?, string?, string?, string?, string?, string?)

Creates a new instance.

    public PostgresException(string messageText, string severity, string invariantSeverity, string sqlState, string? detail = null, string? hint = null, int position = 0, int internalPosition = 0, string? internalQuery = null, string? where = null, string? schemaName = null, string? tableName = null, string? columnName = null, string? dataTypeName = null, string? constraintName = null, string? file = null, string? line = null, string? routine = null)

#### Parameters

`messageText` string
`severity` string
`invariantSeverity` string
`sqlState` string
`detail` string
`hint` string
`position` int
`internalPosition` int
`internalQuery` string
`where` string
`schemaName` string
`tableName` string
`columnName` string
`dataTypeName` string
`constraintName` string
`file` string
`line` string
`routine` string

## Properties

### ColumnName

If the error was associated with a specific table column, the name of the column. (Refer to the schema and table name fields to identify the table.)

    public string? ColumnName { get; }

#### Property Value

string

#### Remarks

PostgreSQL 9.3 and up.

### ConstraintName

If the error was associated with a specific constraint, the name of the constraint. Refer to fields listed above for the associated table or domain. (For this purpose, indexes are treated as constraints, even if they weren't created with constraint syntax.)

    public string? ConstraintName { get; }

#### Property Value

string

#### Remarks

PostgreSQL 9.3 and up.

### DataTypeName

If the error was associated with a specific data type, the name of the data type. (Refer to the schema name field for the name of the data type's schema.)

    public string? DataTypeName { get; }

#### Property Value

string

#### Remarks

PostgreSQL 9.3 and up.

### Detail

An optional secondary error message carrying more detail about the problem. May run to multiple lines.

    public string? Detail { get; }

#### Property Value

string

### File

The file name of the source-code location where the error was reported.

    public string? File { get; }

#### Property Value

string

#### Remarks

PostgreSQL 9.3 and up.

### Hint

An optional suggestion what to do about the problem. This is intended to differ from Detail in that it offers advice (potentially inappropriate) rather than hard facts. May run to multiple lines.

    public string? Hint { get; }

#### Property Value

string

### InternalPosition

This is defined the same as the Position field, but it is used when the cursor position refers to an internally generated command rather than the one submitted by the client. The InternalQuery field will always appear when this field appears. 0 means not provided.

    public int InternalPosition { get; }

#### Property Value

int

### InternalQuery

The text of a failed internally-generated command. This could be, for example, a SQL query issued by a PL/pgSQL function.

    public string? InternalQuery { get; }

#### Property Value

string

### InvariantSeverity

Severity of the error or notice, not localized. Always present since PostgreSQL 9.6.

    public string InvariantSeverity { get; }

#### Property Value

string

### IsTransient

Specifies whether the exception is considered transient, that is, whether retrying the operation could succeed (e.g. a network error). Check SqlState.

    public override bool IsTransient { get; }

#### Property Value

bool

### Line

The line number of the source-code location where the error was reported.

    public string? Line { get; }

#### Property Value

string

### MessageText

The primary human-readable error message. This should be accurate but terse.

    public string MessageText { get; }

#### Property Value

string

#### Remarks

Always present.

### Position

The field value is a decimal ASCII integer, indicating an error cursor position as an index into the original query string. The first character has index 1, and positions are measured in characters not bytes. 0 means not provided.

    public int Position { get; }

#### Property Value

int

### Routine

The name of the source-code routine reporting the error.

    public string? Routine { get; }

#### Property Value

string

### SchemaName

If the error was associated with a specific database object, the name of the schema containing that object, if any.

    public string? SchemaName { get; }

#### Property Value

string

#### Remarks

PostgreSQL 9.3 and up.

### Severity

Severity of the error or notice. Always present.

    public string Severity { get; }

#### Property Value

string

### SqlState

The SQLSTATE code for the error.

    public override string SqlState { get; }

#### Property Value

string

#### Remarks

Always present. Constants are defined in PostgresErrorCodes. See https://www.postgresql.org/docs/current/static/errcodes-appendix.html

### TableName

Table name: if the error was associated with a specific table, the name of the table. (Refer to the schema name field for the name of the table's schema.)

    public string? TableName { get; }

#### Property Value

string

#### Remarks

PostgreSQL 9.3 and up.

### Where

An indication of the context in which the error occurred. Presently this includes a call stack traceback of active PL functions. The trace is one entry per line, most recent first.

    public string? Where { get; }

#### Property Value

string

## Methods

### GetObjectData(SerializationInfo, StreamingContext)

Populates a SerializationInfo with the data needed to serialize the target object.

    [Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.")]
    public override void GetObjectData(SerializationInfo info, StreamingContext context)

#### Parameters

`info` SerializationInfo

  The SerializationInfo to populate with data.

`context` StreamingContext

  The destination (see StreamingContext) for this serialization.

### ToString()

Returns a string that contains the HRESULT of the error.

    public override string ToString()

#### Returns

string

  A string that represents the HRESULT.