go-micro.dev/v5@v5.12.0/internal/website/docs/examples/store-postgres.md (about)

     1  ---
     2  layout: default
     3  ---
     4  
     5  # State with Postgres Store
     6  
     7  Use the Postgres store for persistent key/value state.
     8  
     9  ## In code
    10  
    11  ```go
    12  package main
    13  
    14  import (
    15      "log"
    16      "go-micro.dev/v5"
    17      "go-micro.dev/v5/store"
    18      postgres "go-micro.dev/v5/store/postgres"
    19  )
    20  
    21  func main() {
    22      st := postgres.NewStore()
    23      svc := micro.NewService(micro.Store(st))
    24      svc.Init()
    25  
    26      _ = store.Write(&store.Record{Key: "foo", Value: []byte("bar")})
    27      recs, _ := store.Read("foo")
    28      log.Println("value:", string(recs[0].Value))
    29  
    30      svc.Run()
    31  }
    32  ```
    33  
    34  ## Via environment
    35  
    36  Run your service with env vars set:
    37  
    38  ```bash
    39  MICRO_STORE=postgres \
    40  MICRO_STORE_ADDRESS=postgres://user:pass@127.0.0.1:5432/postgres \
    41  MICRO_STORE_DATABASE=micro \
    42  MICRO_STORE_TABLE=micro \
    43  go run main.go
    44  ```