Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Default ¶
Default returns the default *pgxpool.Pool instance created by InitDefault.
Example ¶
ExampleDefault shows how to retrieve the singleton *pgxpool.Pool created by InitDefault. Returns nil if InitDefault has not been called yet.
package main
import (
"github.com/phcp-tech/common-library-golang/dbsqlc/postgres"
)
func main() {
pool := postgres.Default()
_ = pool // pass to sqlc-generated Queries
}
Output:
func InitDefault ¶
InitDefault initializes the default singleton PostgreSQL connection pool.
Example ¶
ExampleInitDefault shows the singleton pattern for the default PostgreSQL pool. Call InitDefault once at application startup. Subsequent calls are silently ignored (sync.Once).
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/dbsqlc/postgres"
)
func main() {
err := postgres.InitDefault(&postgres.Config{
Host: "127.0.0.1",
Port: "19999",
Database: "mydb",
Username: "user",
Password: "pass",
MaxOpenConns: 10,
MaxIdleConns: 2,
ConnMaxLifetime: 60,
ConnMaxIdletime: 10,
})
fmt.Println(err)
}
Output: <nil>
func NewPostgres ¶
NewPostgres opens a *pgxpool.Pool connection to PostgreSQL and configures the connection pool. It returns the pgx native pool so sqlc-generated Queries (sql_package: "pgx/v5") can use it directly.
Example ¶
ExampleNewPostgres shows that pool creation is lazy: pgxpool.NewWithConfig does not establish any connections at creation time, so no live PostgreSQL server is required. Connections are acquired on first use.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/dbsqlc/postgres"
)
func main() {
pool, err := postgres.NewPostgres(&postgres.Config{
Host: "127.0.0.1",
Port: "19999",
Database: "mydb",
Username: "user",
Password: "pass",
MaxOpenConns: 10,
MaxIdleConns: 2,
ConnMaxLifetime: 60,
ConnMaxIdletime: 10,
})
if err != nil {
fmt.Println("error:", err)
return
}
defer pool.Close()
fmt.Println("pool created")
}
Output: pool created
Types ¶
type Config ¶
type Config struct {
// Database connection parameters
Host string
Port string
Database string
Username string
Password string
// Connection pool parameters
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime int
ConnMaxIdletime int
// Optional search_path to set for all connections in the pool
SearchPath string
}