A small Go package exposing PostgreSQL SQLSTATE constants as typed string values.
It is useful when you want to check or compare database error codes without manually retyping the SQLSTATE values from PostgreSQL documentation.
- PostgreSQL SQLSTATE constants for common and advanced error classes
- Easy-to-use string constants
- Compatible with Go modules
- No runtime dependency beyond the Go standard library
go get github.com/b0r1ssh/pgcodepackage main
import (
"fmt"
"github.com/b0r1ssh/pgcode"
)
func main() {
fmt.Println(pgcode.UniqueViolation) // 23505
fmt.Println(pgcode.SyntaxError) // 42601
fmt.Println(pgcode.ConnectionException)
}pgcode.UniqueViolation
pgcode.NotNullViolation
pgcode.ForeignKeyViolation
pgcode.InvalidPassword
pgcode.TimeoutExpired
pgcode.ConnectionException
pgcode.SyntaxErrorThe package includes constants for the major PostgreSQL SQLSTATE classes and many commonly used codes, including:
- Class 00: successful completion
- Class 08: connection exceptions
- Class 22: data exceptions
- Class 23: integrity constraint violations
- Class 42: syntax errors and invalid object references
- and many more PostgreSQL-specific states
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome. If you want to improve the list of SQLSTATE values or documentation, feel free to open a pull request.