Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DSN ¶
DSN builds a libpq-style connection string from conf.
Example ¶
ExampleDSN shows how to build a libpq-style connection string, including an optional search_path.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/dbsqlx/postgres"
)
func main() {
dsn, err := postgres.DSN(&postgres.Config{
Host: "localhost",
Port: "5432",
Database: "mydb",
Username: "user",
Password: "pass",
SearchPath: "myschema",
})
fmt.Println(err == nil)
fmt.Println(dsn)
}
Output: true host=localhost port=5432 user=user password=pass dbname=mydb sslmode=disable TimeZone=UTC search_path=myschema
func InitDefault ¶
InitDefault opens PostgreSQL and stores it as the dbsqlx default database.
Example ¶
ExampleInitDefault shows the default-instance pattern. Call InitDefault once at application startup; dbsqlx.Default() returns the shared *sqlx.DB for the process.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/dbsqlx/postgres"
)
func main() {
err := postgres.InitDefault(&postgres.Config{
Host: "127.0.0.1",
Port: "1",
Database: "mydb",
Username: "user",
Password: "pass",
})
fmt.Println(err != nil) // true — server unreachable
}
Output:
func NewPostgres ¶
NewPostgres opens a PostgreSQL-backed *sqlx.DB via the pgx stdlib driver. Open verifies connectivity with an eager ping — a non-nil error is returned immediately when the database is unreachable.
Example ¶
ExampleNewPostgres shows how to open a PostgreSQL-backed *sqlx.DB. Open verifies connectivity with an eager ping and runs SHOW search_path — a non-nil error is returned immediately when the server is unreachable.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/dbsqlx/postgres"
)
func main() {
_, err := postgres.NewPostgres(&postgres.Config{
Host: "127.0.0.1",
Port: "1",
Database: "mydb",
Username: "user",
Password: "pass",
})
fmt.Println(err != nil) // true — server unreachable, Open pings eagerly
}
Output: true
Types ¶
type Config ¶
type Config struct {
// Required connection settings.
Host string
Port string
Database string
Username string
Password string
SearchPath string
// Pool tuning; zero values fall back to dbsqlx package defaults.
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime int
ConnMaxIdletime int
}
Config contains PostgreSQL connection settings.