[Just launched] Neon Snapshots let you save critical database states instantly—even for multi-TB datasets

Connect a Go application to Neon

Set up a Neon project in seconds and connect from a Go application

To connect to Neon from a Go application:

  1. Create a Neon project

    If you do not have one already, create a Neon project. Save your connection details including your password. They are required when defining connection settings.

    To create a Neon project:

    1. Navigate to the Projects page in the Neon Console.
    2. Click New Project.
    3. Specify your project settings and click Create Project.
  2. Configure Go application connection settings

    Connecting to Neon requires configuring connection settings in your Go project's .go file.

    note

    Neon is fully compatible with the sql/db package and common Postgres drivers, such as pgx.

    Specify the connection settings in your .go file, as shown in the following example:

    package main
    
    import (
        "context"
        "database/sql"
        "fmt"
    
        _ "github.com/jackc/pgx/v5/stdlib"
    )
    
    func main() {
        connStr := "postgresql://[user]:[password]@[neon_hostname]/[dbname]?sslmode=require"
        db, err := sql.Open("postgres", connStr)
        if err != nil {
            panic(err)
        }
        defer db.Close()
    
        var version string
        if err := db.QueryRow("select version()").Scan(&version); err != nil {
            panic(err)
        }
    
        fmt.Printf("version=%s\n", version)
    }

    Alternatively, you can use the native pgx driver without the database/sql abstraction:

    package main
    
    import (
        "context"
        "fmt"
    
        "github.com/jackc/pgx/v5"
    )
    
    func main() {
        connStr := "postgresql://[user]:[password]@[neon_hostname]/[dbname]?sslmode=require"
        conn, err := pgx.Connect(context.Background(), connStr)
        if err != nil {
            panic(err)
        }
        defer conn.Close(context.Background())
    
        var version string
        err = conn.QueryRow(context.Background(), "select version()").Scan(&version)
        if err != nil {
            panic(err)
        }
    
        fmt.Printf("version=%s\n", version)
    }

    You can find your database connection details by clicking the Connect button on your Project Dashboard. For more information, see Connect from any application.

Need help?

Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more details, see Getting Support.

Last updated on

Was this page helpful?