极客油画

pg的编译安装

编译可重定位版本的postgresql:

LDFLAGS='-Wl,-rpath='"'"'$$ORIGIN/../lib'"'"',--disable-new-dtags'  ./configure '--prefix=/root/pgsql' '--with-systemd' '--with-extra-version=iadm' '--with-system-tzdata=/usr/share/zoneinfo' '--disable-rpath'

其中,编译用的CFLAGS如下:

CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2

可重定位编译常见的排查问题的手段:

  • ldd,可以打印二进制文件所依赖的共享库。
    • 注意:在不受信任的二进制文件上执行ldd是危险的,因为它会执行所探测的程序
  • readelf --dynamic binary_file_path,检查二进制文件是否已设置RUNPATH的值。
    • 可重定位编译的关键是,为RUNPATH设置相对路径。

问题及解决方法

问题1:

configure: error: Package requirements (icu-uc icu-i18n) were not met:No package 'icu-uc' found
No package 'icu-i18n' found

解决办法:

[root@ecs-cs01-0001 postgresql-10.9]# yum install libicu-devel.aarch64

问题2:

configure: error: could not determine flags for linking embedded Perl.
This probably means that ExtUtils::Embed or ExtUtils::MakeMaker is notinstalled.

解决办法:

[root@ecs-cs01-0001 postgresql-10.9]# yum install perl-ExtUtils-Embed -y

问题3:

configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.

解决办法:

[root@ecs-cs01-0001 postgresql-10.9]# yum install -y readline readline-devel

问题4:

configure: error: library 'pam' is required for PAM

解决办法:

[root@ecs-cs01-0001 postgresql-10.9]# yum install -y pam pam-devel

问题5:

configure: error: library 'xml2' (version >= 2.6.23) is required for XML support

解决办法:

[root@ecs-cs01-0001 postgresql-10.9]# yum install -y libxml2 libxml2-devel

问题6:

configure: error: library 'xslt' is required for XSLT support

解决办法:

[root@ecs-cs01-0001 postgresql-10.9]# yum install -y libxslt-devel.aarch64

问题7:

configure: error: library 'ldap' is required for LDAP

解决办法:

[root@ecs-cs01-0001 postgresql-10.9]# yum install -y openldap-devel.aarch64

问题8:

configure: error: header file  is required for systemd support

解决办法:

[root@ecs-cs01-0001 postgresql-10.9]# yum install -y systemd-devel.aarch64

golang中初始化pgsql

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
func runInitPGSubPhase() func(c workflow.RunData) error {
	return func(c workflow.RunData) error {
		data, ok := c.(phasesdata.InstallData)
		if !ok {
			return errors.New("initpg-prepare phase invoked with an invalid data struct")
		}
		cfg := data.Cfg()
		iniconf := cfg.INIConfig
		iniconf.InitConfig()
		rootPath := cfg.RootPath
		pg_bindir := filepath.Join(rootPath, "infra", "pgsql", "bin")
		pg_datadir := filepath.Join(rootPath, "data", "PGData")
		execer := utilsexec.New()

		// 如果 PG_DATADIR 目录不为空,就返回
		if _, err := files.ValidatePathExists(pg_datadir, true); err == nil {
			f, err := os.Open(pg_datadir)
			if err != nil {
				return err
			}
			defer f.Close()
			dirs, err := f.Readdirnames(3)
			if err != io.EOF && len(dirs) > 0 {
				klog.Infoln("pg initialized, pass...")
				return nil
			}
		}

		if runtime.GOOS == "windows" {
			files.NewDir(pg_datadir, true)
			aclCmd := execer.Command("icacls", pg_datadir, "/t", "/q", "/grant", "Everyone:F")
			if err := aclCmd.Run2(0); err != nil {
				return err
			}

			defer func() {
				aclCmd := execer.Command("icacls", pg_datadir, "/reset", "/t", "/c", "/q")
				if err := aclCmd.Run2(0); err != nil {
					klog.Infoln("icacls reset fail:", err)
				}
			}()
		}

		db_driver := iniconf.GetConfValue("db_driver")
		if db_driver != "postgres" {
			klog.InfoS("不是pg数据库,跳过初始化pg数据库")
			return nil
		}

		// 设置PATH环境变量
		setPATH(pg_bindir)

		// 设置环境变量
		PGTZ := iniconf.GetConfValue("tz")
		PGUSER := iniconf.GetConfValue("pg_user")
		PGPASSWORD := xini.Decrypt(iniconf.GetConfValue("pg_password"))
		PGPORT := iniconf.GetConfValue("pg_listen_port")
		PGHOST := iniconf.GetConfValue("pg_service_host")
		envlist := os.Environ()
		envlist = append(envlist, "PGCLIENTENCODING=UTF8", "PGTZ="+PGTZ, "TZ="+PGTZ,
			"PGUSER="+PGUSER, "PGPASSWORD="+PGPASSWORD,
			"PGPORT="+PGPORT, "PGHOST="+PGHOST)

		pwdfile := filepath.Join(rootPath, "data", PGUSER+"pwd")
		if err := os.WriteFile(pwdfile, []byte(PGPASSWORD), os.FileMode(0755)); err != nil {
			return err
		}
		defer func() {
			os.RemoveAll(pwdfile)
		}()

		initCmd := execer.Command(constants.InitdbBinary, "-D", pg_datadir, "-E", "UTF8", "--no-locale",
			"-A", "md5", "-U", PGUSER, "--pwfile="+pwdfile)
		initCmd.SetDir(pg_bindir)
		initCmd.SetEnv(envlist)

		if runtime.GOOS == "linux" {
			// 让子进程以另一个用户身份运行
			install_user := iniconf.GetConfValue("install_user")
			installU, errr := user.Lookup(install_user)
			if errr == nil {
				initCmd.SetUser(installU)
			} else {
				klog.InfoS("lookup "+install_user+" user fail", "err", errr)
			}
		}

		if err := initCmd.Run2(0); err != nil {
			os.RemoveAll(pg_datadir)
			return errors.Wrap(err, initCmd.String()+" fail")
		}
		return nil
	}
}

func setPATH(paths ...string) {
	if len(paths) == 0 {
		return
	}

	// 获取当前的PATH环境变量值
	currentPath := os.Getenv("PATH")

	// 确保新路径前后都有分隔符(Windows使用';',Unix-like使用':')
	var separator string
	if runtime.GOOS == "windows" {
		separator = ";"
	} else {
		separator = ":"
	}
	if !strings.HasSuffix(currentPath, separator) {
		currentPath += separator
	}
	newPath := currentPath + strings.Join(paths, separator)

	// 设置环境变量,注意:这仅影响当前进程及由当前进程创建的子进程
	os.Setenv("PATH", newPath)
}

pg的常用配置

postgresql.conf.tpl:

listen_addresses                 = '{{.pg_listen_host}}'
port                             = {{.pg_listen_port}}
unix_socket_directories          = '../../data/PGData'
fsync                            = on{{if eq .os "windows"}}
shared_memory_type = windows
dynamic_shared_memory_type = windows{{end}}
shared_buffers                   = {{index_dynamic .pg_shared_buffers .env_deploy_resource_mode}}
temp_buffers                     = 8MB
work_mem                         = {{index_dynamic .pg_work_mem .env_deploy_resource_mode}}
huge_pages                       = off
effective_cache_size             = 4GB
maintenance_work_mem             = {{index_dynamic .pg_maintenance_work_mem .env_deploy_resource_mode}}
max_connections                  = {{index_dynamic .pg_max_connections .env_deploy_resource_mode}}
max_prepared_transactions        = {{index_dynamic .pg_max_connections .env_deploy_resource_mode}}
superuser_reserved_connections   = 2
#tcp_keepalived_idle              = 120
#tcp_keepalived_interval          = 10
#tcp_keepalived_count             = 10
authentication_timeout           = 10s
#wal_level                       = replica
wal_level                        = minimal
max_wal_senders                  = 0
wal_buffers                      = {{index_dynamic .pg_wal_buffers .env_deploy_resource_mode}}
checkpoint_completion_target     = 0.9
commit_delay                     = {{index_dynamic .pg_commit_delay .env_deploy_resource_mode}}
commit_siblings                  = 4
wal_log_hints                    = on
max_wal_size                     = 1GB
min_wal_size                     = 80MB
#wal_keep_segments               = 640
logging_collector                = on
log_destination                  = 'csvlog'
log_directory                    = '../../logs/infra/pgsql'
log_filename                     = 'postgresql-%a.log'
log_rotation_age                 = 1d
log_duration                     = off
log_truncate_on_rotation         = on
log_min_duration_statement       = 60000
log_checkpoints                  = off
#log_conenctions                  = off
log_disconnections               = off
log_lock_waits                   = off
log_statement                    = none
log_line_prefix                  = '%t [%p]: user=%u,db=%d,client=%h '
log_timezone                     = '{{.tz}}'
timezone                         = '{{.tz}}'
log_min_messages                 = 'error'
log_min_error_statement          = 'error'
#clilent_min_messages             = 'error'
#shared_preload_libraries         = 'pg_stat_statements'
#pg_stat_statements.max           = 10000
#pg_stat_statements.track         = all
#pg_stat_statements.track_utility = off
#pg_stat_statements.save          = off

ipostgres.service:

[Unit]
Description=PostgreSQL 12 database server
Documentation=https://www.postgresql.org/docs/12/static
After=syslog.target
After=network.target

[Service]
Environment="PGDATA={{.root_dir}}/data/PGData"
Environment="LD_LIBRARY_PATH={{.root_dir}}/infra/pgsql/lib"
Environment="PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj"
Environment="PG_OOM_ADJUST_VALUE=0"
ExecStop={{.root_dir}}/infra/pgsql/bin/pg_ctl stop -D ${PGDATA}
ExecStart={{.root_dir}}/infra/pgsql/bin/pg_ctl start -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
OOMScoreAdjust=-1000
Restart=on-failure
RestartSec=5s
Type=forking
User={{.install_user}}

专题:

本文发表于 2022-01-01,最后修改于 2022-01-01。

本站永久域名「 jiavvc.top 」,也可搜索「 极客油画 」找到我。


上一篇 « swim 下一篇 » pg高可用

赞赏支持

请我吃鸡腿 =^_^=

i ysf

云闪付

i wechat

微信

推荐阅读

Big Image