go 修改postgresql的配置参数

news/2024/7/9 19:32:05 标签: golang, postgresql, 开发语言

postgresqlconfpostgresqlautoconf_0">postgresql.conf与postgresql.auto.conf的区别

  • postgresql.auto.conf的优先级高于postgresql.conf,如果一个参数同时存在postgresql.auto.conf和postgresql.conf里面,系统会先读postgresql.auto.conf的参数配置。

  • 使用alter system set修改的是postgresql.auto.conf文件的内容,postgresql.conf则是通过文本编辑方式修改。比如执行alter system set max_wal_size=default将参数设回 default 时,postgresql.auto.conf文件里的max_wal_size这项配置会被删除,重新用回postgresql.conf文件的设置。

  • postgresql.conf文件的参数后面有# (change requires restart),表示必须重启才能生效,使用select pg_reload_conf()或pg_ctl reload不行。

执行alter system set max_wal_size=2500;
发现修改的是postgresql.auto.conf文件
执行select pg_reload_conf();同样的参数,优先加载的是postgresql.auto.conf文件里面的参数配置
重启postgresql后,同样的参数,优先使用的postgresql.auto.conf文件里面的参数配置
手工修改postgresql.auto.conf文件,执行select pg_reload_conf()会加载postgresql.auto.conf文件
手工修改postgresql.auto.conf文件,重启postgresql会加载postgresql.auto.conf文件

go的代码样例

func main() {
	// 连接到 PostgreSQL 数据库
	dsn := "host=localhost user=user password=password  dbname=postgres port=5432 sslmode=disable TimeZone=Asia/Shanghai"
	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		sqlDB, err := db.DB()
		if err != nil {
			log.Fatal(err)
		}
		sqlDB.Close()
	}()

	cmd := "ALTER SYSTEM SET array_nulls= 'off' "
	_, err = db.Raw(cmd).Rows()
	if err != nil {
		return
	}
	cmdReload := "SELECT pg_reload_conf()"
	_, err = db.Raw(cmdReload).Rows()
	if err != nil {
		fmt.Printf("执行sql[%s]失败:%v", cmdReload, err)
		return
	}

	cfg, err := ini.LoadSources(ini.LoadOptions{AllowShadows: true}, "/var/lib/pgsql/12/data/postgresql.auto.conf")
	// 获取指定的 Section
	section, err := cfg.GetSection("DEFAULT")
	if err != nil {
		log.Fatal(err)
	}
	keyToCheck := "array_nulls"

	// 检查 key 是否存在
	if section.HasKey(keyToCheck) {
		fmt.Printf("Key '%s' exists in the section.\n", keyToCheck)
	} else {
		fmt.Printf("Key '%s' does not exist in the section.\n", keyToCheck)
	}
}


http://www.niftyadmin.cn/n/5313794.html

相关文章

阿里巴巴秋招前端笔试题

单选题 下面的 JSX 代码中&#xff0c;哪一个无法达到预期的效果&#xff1f; A.<h2>Hello World</h2> B.<input type”checkbox”/> C.<div class”msg-box”>{msg}</div> D.<label htmlFor”name”>Leo</label> E.div styl…

C++系列十六:枚举

枚举 一、C枚举基础 在C中&#xff0c;枚举&#xff08;Enumeration&#xff09;是一种用户定义的数据类型&#xff0c;它包含一组整数值&#xff0c;每个值都与一个标识符关联。通过使用枚举&#xff0c;我们可以使代码更加清晰易懂&#xff0c;避免使用魔术数字或字符串。 …

linux离线安装git

首先需要有一台有网的linux&#xff0c;下载git源码包 下载地址 Index of /pub/software/scm/git/ 我这里下载的是git-2.9.5.tar.gz&#xff0c;依次执行如下命令进行编译 # 在有网的环境中&#xff0c;编译git&#xff0c;然后打包拿到内网直接用 # 下载git-2.9.5.tar.gz&…

深度学习 基本理论 3 :物体检测(Anchor base/NMS/softmax/损失函数/BCE/CE/zip

1、 Anchor base和Anchor free 1.1 Anchor base Anchor base&#xff0c;译为基于锚点的一个物体检测方法&#xff0c;也可以叫做基于一组预定义的框模型会预测每个锚点是否存在对象&#xff0c;并预测需要对锚点进行的调整以更好地适应该对象Anchor base物体检测方法&#x…

比较与排序(二)排序

一、介绍&#xff1a; 1、java比较器&#xff1a;Java中的对象&#xff0c;正常情况下&#xff0c;只能进行比较&#xff1a; 或 ! 。不能使用 > 或 <&#xff0c;但是在开发场景中&#xff0c;我们需要对多个对象进行排序&#xff0c;即比较对象的大小。java实现比较器…

【操作系统xv6】学习记录2 -RISC-V Architecture

说明&#xff1a;看完这节&#xff0c;不会让你称为汇编程序员&#xff0c;知识操作系统的前置。 ref&#xff1a;https://binhack.readthedocs.io/zh/latest/assembly/mips.html https://www.bilibili.com/video/BV1w94y1a7i8/?p7 MIPS MIPS的意思是 “无内部互锁流水级的微…

react antd 计算公式 (+-*/)运算,回显

计算器的源码计算器触发事件源码 import { DictValueEnumObj } from /components/DictTag; import { getDeptTree, getFormulaListAll, getListAll } from /services/Energy/Metering;import { getListAllInfo, getDepartmentName } from /services/Energy/Calculation; import…

Golang 快乐数 leetcode202 map哈希表 快慢指针法

快乐数 leetcode202 利用map记录 对于本体中快乐数的检测&#xff0c;如果非快乐数&#xff0c;应该会在循环中有重复的数出现。 当为3位数时&#xff0c;即使为999&#xff0c;每位的平方和仅为243&#xff0c;所以能循环的数肯定是有限的。我们使用map进行数据的记录&#…