package pg import ( "context" "fmt" "github.com/jackc/pgx/v5/pgxpool" "sync" ) type Postgres struct { db *pgxpool.Pool } var ( pgInstance *Postgres pgOnce sync.Once ) func NewPgPool( ctx context.Context, username, password, host, port, dbname string, ) *Postgres { connString := "postgres://" + username + ":" + password + "@" + host + ":" + port + "/" + dbname + "?pool_max_conns=100" pgOnce.Do(func() { db, err := pgxpool.New(ctx, connString) if err != nil { panic(err) } pgInstance = &Postgres{db} }) return pgInstance } func (pg *Postgres) CreateTable(ctx context.Context) error { result, err := pg.db.Exec(ctx, createTableQuery) if err != nil { return err } fmt.Println(result) return nil } func (pg *Postgres) DropTable(ctx context.Context) error { result, err := pg.db.Exec(ctx, dropTableQuery) if err != nil { return err } fmt.Println(result) return nil } func (pg *Postgres) Exec(ctx context.Context, query string, args ...interface{}) (interface{}, error) { result, err := pg.db.Exec(ctx, query, args...) if err != nil { return nil, err } return result, nil } func (pg *Postgres) Query(ctx context.Context, query string, args ...interface{}) (interface{}, error) { result, err := pg.db.Query(ctx, query, args...) if err != nil { return nil, err } return result, nil } func (pg *Postgres) QueryRow(ctx context.Context, query string, args ...interface{}) interface{} { result := pg.db.QueryRow(ctx, query, args...) return result }