Jackc/pgx: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(8 intermediate revisions by the same user not shown)
Line 8: Line 8:
=Overview=
=Overview=
<code>jackc/pgx</code> 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 <code>[[Go_Package_database|database/sql]]</code> package, but using <code>database/sql</code> is optional. It understands PostgreSQL data types. <code>jackc/pgx</code> is recommended when the application '''only''' targets PostgreSQL.
<code>jackc/pgx</code> 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 <code>[[Go_Package_database|database/sql]]</code> package, but using <code>database/sql</code> is optional. It understands PostgreSQL data types. <code>jackc/pgx</code> is recommended when the application '''only''' targets PostgreSQL.
=Installation=
<syntaxhighlight lang='bash'>
go get github.com/jackc/pgx/v5
</syntaxhighlight>
=Programming Model=
See: {{Internal|PostgreSQL_Support_in_Go#With_jackc/pgx|PostgreSQL Support in Go}}


=Database Operations=
==Create a Schema==
==Open a Connection==
<syntaxhighlight lang='go'>
sql := `CREATE SCHEMA IF NOT EXISTS blue`
commandTag, err := conn.Exec(ctx, sql)
if err != nil {
  ...
} else {
  fmt.Printf("%s\n", commandTag)
}
</syntaxhighlight>
==Create a Table==
<syntaxhighlight lang='go'>
sql := `CREATE TABLE IF NOT EXISTS BLUE.TEST ("ID" int, "NAME" varchar(10))`
commandTag, err = db.Exec(sql)
if err != nil {
...
} else {
  fmt.Printf("%s\n", commandTag)
}
</syntaxhighlight>
 
==Delete a Table==
 
==Insert a Row==
<syntaxhighlight lang='go'>
commandTag, err := conn.Exec(context.B, `INSERT INTO someschema.sometable ("id", "color", "size") VALUES ($1, $2, $3)`, 1, "red", 15)
if err != nil {
    ...
} else {
    fmt.Printf("%s\n", commandTag)
}
</syntaxhighlight>
 
==Query==
<syntaxhighlight lang='go'>
rows, err := conn.Query(ctx, `SELECT "id", "color", "lab_id", "size" FROM someschema.sometable`)
if err != nil {
    ...
}
for rows.Next() {
    var (
      id    string
  color string
  size  string
)
    err = rows.Scan(&id, &color, &size)
    if err != nil {
        ...
} else {
    fmt.Printf("%s, %s, %s, %s, %v\n", id, fleetID, labID, category, keys)
    }
}
</syntaxhighlight>

Latest revision as of 22:27, 30 May 2024

External

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

Programming Model

See:

PostgreSQL Support in Go

Create a Schema

sql := `CREATE SCHEMA IF NOT EXISTS blue`
commandTag, err := conn.Exec(ctx, sql)
if err != nil {
  ...
} else {
  fmt.Printf("%s\n", commandTag)
}

Create a Table

sql := `CREATE TABLE IF NOT EXISTS BLUE.TEST ("ID" int, "NAME" varchar(10))`
commandTag, err = db.Exec(sql)
if err != nil {
 ...
} else {
  fmt.Printf("%s\n", commandTag)
}

Delete a Table

Insert a Row

commandTag, err := conn.Exec(context.B, `INSERT INTO someschema.sometable ("id", "color", "size") VALUES ($1, $2, $3)`, 1, "red", 15)
if err != nil {
    ...
} else {
    fmt.Printf("%s\n", commandTag)
}

Query

rows, err := conn.Query(ctx, `SELECT "id", "color", "lab_id", "size" FROM someschema.sometable`)
if err != nil {
    ...
}
for rows.Next() {
    var (
	      id    string
		  color string
		  size  string
		)
    err = rows.Scan(&id, &color, &size)
    if err != nil {
        ...
	} else {
   	   fmt.Printf("%s, %s, %s, %s, %v\n", id, fleetID, labID, category, keys)
    }
}