Jackc/pgx: Difference between revisions
Jump to navigation
Jump to search
Line 15: | Line 15: | ||
=Database Operations= | =Database Operations= | ||
==Open a Connection== | ==Open a Connection== | ||
<syntaxhighlight lang='go'> | |||
import ( | |||
"context" | |||
"fmt" | |||
"os" | |||
"github.com/jackc/pgx/v5" | |||
) | |||
... | |||
ctx := context.Background() | |||
connectionString := "postgres://postgres:@localhost:5432" | |||
conn, err := pgx.Connect(ctx, connectionString) | |||
if err != nil { | |||
... | |||
} | |||
defer func() { | |||
if err = conn.Close(ctx); err != nil { | |||
... | |||
} | |||
}() | |||
if err = conn.Ping(ctx); err != nil { | |||
... | |||
} | |||
fmt.Printf("success\n") | |||
</syntaxhighlight> |
Revision as of 22:12, 21 May 2024
External
- https://github.com/jackc/pgx
- https://github.com/jackc/pgx/wiki/Getting-started-with-pgx
- https://www.youtube.com/watch?v=sXMSWhcHCf8
Internal
Overview
jackc/pgx
is a PostgreSQL low-level, high performance driver and toolkit that exposes PostgreSQL-specific features such as LISTEN/NOTIFY and COPY. It also includes an adapter for the standard database/sql
package, but using database/sql
is optional. It understands PostgreSQL data types. jackc/pgx
is recommended when the application only targets PostgreSQL.
Installation
go get github.com/jackc/pgx/v5
Database Operations
Open a Connection
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v5"
)
...
ctx := context.Background()
connectionString := "postgres://postgres:@localhost:5432"
conn, err := pgx.Connect(ctx, connectionString)
if err != nil {
...
}
defer func() {
if err = conn.Close(ctx); err != nil {
...
}
}()
if err = conn.Ping(ctx); err != nil {
...
}
fmt.Printf("success\n")