/ kvdb / postgres / db.go
db.go
 1  //go:build kvdb_postgres
 2  
 3  package postgres
 4  
 5  import (
 6  	"context"
 7  
 8  	"github.com/btcsuite/btcwallet/walletdb"
 9  	"github.com/lightningnetwork/lnd/kvdb/sqlbase"
10  )
11  
12  // sqliteCmdReplacements defines a mapping from some SQLite keywords and phrases
13  // to their postgres counterparts.
14  var sqliteCmdReplacements = sqlbase.SQLiteCmdReplacements{
15  	"BLOB":                "BYTEA",
16  	"INTEGER PRIMARY KEY": "BIGSERIAL PRIMARY KEY",
17  }
18  
19  // newPostgresBackend returns a db object initialized with the passed backend
20  // config. If postgres connection cannot be established, then returns error.
21  func newPostgresBackend(ctx context.Context, config *Config, prefix string) (
22  	walletdb.DB, error) {
23  
24  	cfg := &sqlbase.Config{
25  		DriverName:            "pgx",
26  		Dsn:                   config.Dsn,
27  		Timeout:               config.Timeout,
28  		Schema:                "public",
29  		TableNamePrefix:       prefix,
30  		SQLiteCmdReplacements: sqliteCmdReplacements,
31  		WithTxLevelLock:       config.WithGlobalLock,
32  	}
33  
34  	return sqlbase.NewSqlBackend(ctx, cfg)
35  }