Skip to main content
Version: current

PostgreSQL

Relational Databasebackend/postgresql

PostgreSQL is a powerful, open source object-relational database system with a strong reputation for reliability, feature robustness, and performance. It supports both SQL and JSON querying.

📦 Installation

go get github.com/azrod/kivigo/backend/postgresql

Requires a running PostgreSQL server. You can use PostgreSQL locally, in Docker, or cloud services like AWS RDS, Google Cloud SQL, or Azure Database for PostgreSQL.

Dependencies

  • PostgreSQL server (12 or later recommended)
  • PostgreSQL database and table created
  • Valid connection credentials with appropriate permissions

✨ Features

Basic Operations
Batch Operations
Health Checks
ACID Transactions
JSON Support
Advanced Indexing
Streaming Replication
Extensions

🚀 Usage

package main

import (
"time"

"github.com/azrod/kivigo"
"github.com/azrod/kivigo/backend/postgresql"
)

func main() {
// Basic configuration
opt := postgresql.NewOptions()
opt.DataSourceName = "postgres://user:password@localhost:5432/kivigo?sslmode=disable"
opt.TableName = "keyvalue"
opt.KeyColumn = "key"
opt.ValueColumn = "value"

// Advanced configuration with SSL and connection pool
advOpt := postgresql.NewOptions()
advOpt.DataSourceName = "postgres://user:password@postgres-server:5432/kivigo?sslmode=require&sslcert=client-cert.pem&sslkey=client-key.pem&sslrootcert=ca-cert.pem"
advOpt.TableName = "kv_store"
advOpt.KeyColumn = "id"
advOpt.ValueColumn = "data"
advOpt.CreatedAtColumn = "created_at"
advOpt.UpdatedAtColumn = "updated_at"

// Optional: Configure connection pool
advOpt.MaxOpenConns = 30
advOpt.MaxIdleConns = 10
advOpt.ConnMaxLifetime = 300 * time.Second
advOpt.ConnMaxIdleTime = 90 * time.Second

// Optional: Use JSONB for value storage
advOpt.UseJSONB = true
advOpt.EnableWAL = true

// Create backend
kvStore, err := postgresql.New(advOpt)
if err != nil {
panic(err)
}
defer kvStore.Close()

// Create client
client, err := kivigo.New(kvStore)
if err != nil {
panic(err)
}
}

📝 Notes

  • PostgreSQL JSONB provides better performance than JSON for complex queries
  • Use proper indexes on JSONB columns for optimal query performance
  • Connection pooling is crucial for high-concurrency applications
  • Consider using table partitioning for time-series or large datasets
  • PostgreSQL supports advanced features like window functions and CTEs
  • Use EXPLAIN ANALYZE to optimize query performance
  • WAL (Write-Ahead Logging) provides durability and enables point-in-time recovery
  • PostgreSQL extensions like PostGIS add powerful geospatial capabilities

🔗 Additional Resources