项目作者: quasoft

项目描述 :
Simple Go package for reading/writing to PostgreSQL config files
高级语言: Go
项目地址: git://github.com/quasoft/pgconf.git
创建时间: 2018-07-15T06:45:59Z
项目社区:https://github.com/quasoft/pgconf

开源协议:MIT License

下载


pgconf

pgconf is a simple Go package for reading/writing to PostgreSQL config file (postgresql.conf) that:

  • Preseves existing whitespace and comments
  • Supports single quoted and backslash-quoted values (eg. search_path = '''$user'', \'public\'')
  • Supports values with optional equal sign (eg. logconnections yes)
  • Works with ASCII and UTF-8 .conf files

How to use

postgresql.conf:

To read or update postgresql.conf files use the pgconf/conf package:

  1. import (
  2. "fmt"
  3. "github.com/quasoft/pgconf/conf"
  4. )
  5. func main() {
  6. c, err := conf.Open("/data/postgresql.conf")
  7. if err != nil {
  8. panic("Could not open conf file: " + err.Error())
  9. }
  10. // StringK automatically dequotes values
  11. dest, err := c.StringK("log_destination")
  12. if err != nil || dest != "syslog" {
  13. // If key was not set or has the wrong value
  14. fmt.Println("log_destination value is not what we want, changing it now")
  15. c.SetStringK("log_destination", "syslog") // SetStringK automatically quotes values if necessary
  16. }
  17. err = c.WriteFile("/data/postgresql.conf", 0644)
  18. if err != nil {
  19. panic("Could not save file: " + err.Error())
  20. }
  21. }

pg_hba.conf

To read or update pg_hba.conf files use the pgconf/hba package:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/quasoft/pgconf/hba"
  5. )
  6. func main() {
  7. conf, err := hba.Open("../../hba/testdata/sample.conf") // pg_hba.conf
  8. if err != nil {
  9. panic(fmt.Errorf("Failed opening file pg_hba.conf: %s", err))
  10. }
  11. // Find all rows for replication host-based authentication
  12. rows, err := conf.LookupAll(hba.Database, "replication")
  13. if err != nil {
  14. panic(fmt.Errorf("Failed looking up for replication rows: %s", err))
  15. }
  16. fmt.Printf("Found %d replication rows with addresses as follows:\n", len(rows))
  17. for _, row := range rows {
  18. // Get and print value for ADDRESS column
  19. address, err := conf.String(row, hba.Address)
  20. if err != nil {
  21. panic(fmt.Errorf("Could not read address value: %s", err))
  22. }
  23. fmt.Println(" - " + address)
  24. }
  25. }

outputs:

  1. Found 3 replication rows with addresses as follows:
  2. - 127.0.0.1/32
  3. - ::1/128
  4. - 10.0.0.3/32

Hint

Usually it’s safer to write changes to a temp file and once that writing is over to rename
the temp file to the actual configuration file:

  1. ...
  2. err = c.WriteFile("/data/postgresql.conf.tmp", 0644)
  3. if err != nil {
  4. panic("Could not save file: " + err.Error())
  5. }
  6. err = os.Rename("/data/postgresql.conf.tmp", "/data/postgresql.conf")
  7. if err != nil {
  8. panic("Could not rename tmp file to conf: " + err.Error())
  9. }
  10. ...
  11. }

If you use this approach make sure to store the temp file in a secure location (eg. the data
dir) with restricted permissions and not inside the /tmp directory.