All Projects → go-pg → Pg

go-pg / Pg

Licence: bsd-2-clause
Golang ORM with focus on PostgreSQL features and performance

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to Pg

Go Sqlbuilder
A flexible and powerful SQL string builder library plus a zero-config ORM.
Stars: ✭ 539 (-89.04%)
Mutual labels:  sql, orm, database, postgresql
Rel
💎 Modern Database Access Layer for Golang - Testable, Extendable and Crafted Into a Clean and Elegant API
Stars: ✭ 317 (-93.55%)
Mutual labels:  sql, orm, database, postgresql
Jooq
jOOQ is the best way to write SQL in Java
Stars: ✭ 4,695 (-4.53%)
Mutual labels:  sql, orm, database, postgresql
Qb
The database toolkit for go
Stars: ✭ 524 (-89.35%)
Mutual labels:  sql, orm, database, postgresql
Linq2db
Linq to database provider.
Stars: ✭ 2,211 (-55.04%)
Mutual labels:  sql, orm, database, postgresql
Nut
Advanced, Powerful and easy to use ORM for Qt
Stars: ✭ 181 (-96.32%)
Mutual labels:  sql, orm, database, postgresql
Db
Data access layer for PostgreSQL, CockroachDB, MySQL, SQLite and MongoDB with ORM-like features.
Stars: ✭ 2,832 (-42.42%)
Mutual labels:  sql, orm, database, postgresql
Ansible Role Postgresql
Ansible Role - PostgreSQL
Stars: ✭ 310 (-93.7%)
Mutual labels:  sql, database, postgresql
Evolve
Database migration tool for .NET and .NET Core projects. Inspired by Flyway.
Stars: ✭ 477 (-90.3%)
Mutual labels:  sql, database, postgresql
Freezer
A simple & fluent Android ORM, how can it be easier ? RxJava2 compatible
Stars: ✭ 326 (-93.37%)
Mutual labels:  sql, orm, database
Beam
A type-safe, non-TH Haskell SQL library and ORM
Stars: ✭ 454 (-90.77%)
Mutual labels:  sql, orm, postgresql
Squeal
Squeal, a deep embedding of SQL in Haskell
Stars: ✭ 308 (-93.74%)
Mutual labels:  sql, database, postgresql
Node Orm2
Object Relational Mapping
Stars: ✭ 3,063 (-37.72%)
Mutual labels:  orm, database, postgresql
Sqlc
Generate type-safe code from SQL
Stars: ✭ 4,564 (-7.2%)
Mutual labels:  sql, orm, postgresql
Xo
Command line tool to generate idiomatic Go code for SQL databases supporting PostgreSQL, MySQL, SQLite, Oracle, and Microsoft SQL Server
Stars: ✭ 2,974 (-39.53%)
Mutual labels:  sql, orm, postgresql
Jet
Type safe SQL builder with code generation and automatic query result data mapping
Stars: ✭ 373 (-92.42%)
Mutual labels:  sql, database, postgresql
Pg timetable
pg_timetable: Advanced scheduling for PostgreSQL
Stars: ✭ 382 (-92.23%)
Mutual labels:  sql, database, postgresql
Dbeaver
Free universal database tool and SQL client
Stars: ✭ 23,752 (+382.96%)
Mutual labels:  sql, database, postgresql
Sqlboiler
Generate a Go ORM tailored to your database schema.
Stars: ✭ 4,497 (-8.56%)
Mutual labels:  orm, database, postgresql
Franchise
🍟 a notebook sql client. what you get when have a lot of sequels.
Stars: ✭ 3,823 (-22.27%)
Mutual labels:  sql, database, postgresql

Maintenance mode

go-pg is in a maintenance mode and only critical issues are addressed. New development happens in Bun repo which offers similar functionality but works with PostgreSQL, MySQL, MariaDB, and SQLite.

https://github.com/uptrace/bun




PostgreSQL client and ORM for Golang

Build Status PkgGoDev Documentation Chat

Ecosystem

Features

Installation

go-pg supports 2 last Go versions and requires a Go version with modules support. So make sure to initialize a Go module:

go mod init github.com/my/repo

And then install go-pg (note v10 in the import; omitting it is a popular mistake):

go get github.com/go-pg/pg/v10

Quickstart

package pg_test

import (
    "fmt"

    "github.com/go-pg/pg/v10"
    "github.com/go-pg/pg/v10/orm"
)

type User struct {
    Id     int64
    Name   string
    Emails []string
}

func (u User) String() string {
    return fmt.Sprintf("User<%d %s %v>", u.Id, u.Name, u.Emails)
}

type Story struct {
    Id       int64
    Title    string
    AuthorId int64
    Author   *User `pg:"rel:has-one"`
}

func (s Story) String() string {
    return fmt.Sprintf("Story<%d %s %s>", s.Id, s.Title, s.Author)
}

func ExampleDB_Model() {
    db := pg.Connect(&pg.Options{
        User: "postgres",
    })
    defer db.Close()

    err := createSchema(db)
    if err != nil {
        panic(err)
    }

    user1 := &User{
        Name:   "admin",
        Emails: []string{"admin1@admin", "admin2@admin"},
    }
    _, err = db.Model(user1).Insert()
    if err != nil {
        panic(err)
    }

    _, err = db.Model(&User{
        Name:   "root",
        Emails: []string{"root1@root", "root2@root"},
    }).Insert()
    if err != nil {
        panic(err)
    }

    story1 := &Story{
        Title:    "Cool story",
        AuthorId: user1.Id,
    }
    _, err = db.Model(story1).Insert()
    if err != nil {
        panic(err)
    }

    // Select user by primary key.
    user := &User{Id: user1.Id}
    err = db.Model(user).WherePK().Select()
    if err != nil {
        panic(err)
    }

    // Select all users.
    var users []User
    err = db.Model(&users).Select()
    if err != nil {
        panic(err)
    }

    // Select story and associated author in one query.
    story := new(Story)
    err = db.Model(story).
        Relation("Author").
        Where("story.id = ?", story1.Id).
        Select()
    if err != nil {
        panic(err)
    }

    fmt.Println(user)
    fmt.Println(users)
    fmt.Println(story)
    // Output: User<1 admin [admin1@admin admin2@admin]>
    // [User<1 admin [admin1@admin admin2@admin]> User<2 root [root1@root root2@root]>]
    // Story<1 Cool story User<1 admin [admin1@admin admin2@admin]>>
}

// createSchema creates database schema for User and Story models.
func createSchema(db *pg.DB) error {
    models := []interface{}{
        (*User)(nil),
        (*Story)(nil),
    }

    for _, model := range models {
        err := db.Model(model).CreateTable(&orm.CreateTableOptions{
            Temp: true,
        })
        if err != nil {
            return err
        }
    }
    return nil
}

See also

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].