Skip to content

PostgreSQL Storage

Production-grade storage for multi-process and distributed bot deployments. Uses sqlc-generated queries for type safety.

Install

bash
go get github.com/mtgo-labs/storage/postgres

Usage

go
import (
    "github.com/mtgo-labs/storage/postgres"
    tg "github.com/mtgo-labs/mtgo/telegram"
)

store, err := postgres.Open("postgres://user:pass@localhost:5432/botdb?sslmode=disable")
if err != nil {
    log.Fatal(err)
}
defer store.Close()

client, _ := tg.NewClient(apiID, apiHash, &tg.Config{
    BotToken:    botToken,
    SessionName: "my_bot",
    Storage:     store,
    SavePeers:   true,
})

API

MethodDescription
Open(dsn string)Connect using a PostgreSQL DSN
Close()Close the database connection

The returned *PostgresStorage implements storage.Storage and storage.ConversationStore.

Schema

Tables are created automatically on first connection. The schema mirrors SQLite with PostgreSQL-specific types. Generated by sqlc from storage/sqlc/postgres/queries.sql.

Connection String Format

postgres://user:password@host:port/database?sslmode=disable

Common sslmode values: disable, require, verify-ca, verify-full.

Released under the Apache-2.0 License.