Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InitDefault ¶
InitDefault opens PostgreSQL and stores it as the dbgorm default database.
Example ¶
ExampleInitDefault shows the default-instance pattern. Call InitDefault once at application startup; dbgorm.Default() returns the shared *gorm.DB for the process.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/dbgorm/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 GORM database.
Example ¶
ExampleNewPostgres shows how to open a PostgreSQL GORM database. GORM's PostgreSQL driver pings the server and runs SHOW search_path on Open — a non-nil error is returned when the server is unreachable.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/dbgorm/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, GORM pings on Open
}
Output: true
Types ¶
type Config ¶
type Config struct {
// Required connection settings.
Host string
Port string
Database string
Username string
Password string
SearchPath string
// gorm.Config fields for connection pool settings.
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime int
ConnMaxIdletime int
Logger *slog.Logger
}
Config contains PostgreSQL connection settings.
Click to show internal directories.
Click to hide internal directories.