PostgreSQL的 initdb 源代码分析之十二
postgresql 分析 源代码 十二
2023-09-11 14:20:28 时间
继续分析
/* Now create all the text config files */ setup_config();
将其展开:
实质就是,确定各种参数,分别写入 postgresql.conf 、pg_hba.conf、pg_indent.conf 文件。
/* * set up all the config files */ static void setup_config(void) { char **conflines; char repltok[100]; char path[MAXPGPATH]; fputs(_("creating configuration files ... "), stdout); fflush(stdout); /* postgresql.conf */ conflines = readfile(conf_file); snprintf(repltok, sizeof(repltok), "max_connections = %d", n_connections); conflines = replace_token(conflines, "#max_connections = 100", repltok); if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0) snprintf(repltok, sizeof(repltok), "shared_buffers = %dMB", (n_buffers * (BLCKSZ / 1024)) / 1024); else snprintf(repltok, sizeof(repltok), "shared_buffers = %dkB", n_buffers * (BLCKSZ / 1024)); conflines = replace_token(conflines, "#shared_buffers = 32MB", repltok); #if DEF_PGPORT != 5432 snprintf(repltok, sizeof(repltok), "#port = %d", DEF_PGPORT); conflines = replace_token(conflines, "#port = 5432", repltok); #endif snprintf(repltok, sizeof(repltok), "lc_messages = '%s'", escape_quotes(lc_messages)); conflines = replace_token(conflines, "#lc_messages = 'C'", repltok); snprintf(repltok, sizeof(repltok), "lc_monetary = '%s'", escape_quotes(lc_monetary)); conflines = replace_token(conflines, "#lc_monetary = 'C'", repltok); snprintf(repltok, sizeof(repltok), "lc_numeric = '%s'", escape_quotes(lc_numeric)); conflines = replace_token(conflines, "#lc_numeric = 'C'", repltok); snprintf(repltok, sizeof(repltok), "lc_time = '%s'", escape_quotes(lc_time)); conflines = replace_token(conflines, "#lc_time = 'C'", repltok); switch (locale_date_order(lc_time)) { case DATEORDER_YMD: strcpy(repltok, "datestyle = 'iso, ymd'"); break; case DATEORDER_DMY: strcpy(repltok, "datestyle = 'iso, dmy'"); break; case DATEORDER_MDY: default: strcpy(repltok, "datestyle = 'iso, mdy'"); break; } conflines = replace_token(conflines, "#datestyle = 'iso, mdy'", repltok); snprintf(repltok, sizeof(repltok), "default_text_search_config = 'pg_catalog.%s'", escape_quotes(default_text_search_config)); conflines = replace_token(conflines, "#default_text_search_config = 'pg_catalog.simple'", repltok); snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data); writefile(path, conflines); chmod(path, S_IRUSR | S_IWUSR); free(conflines); /* pg_hba.conf */ conflines = readfile(hba_file); #ifndef HAVE_UNIX_SOCKETS conflines = filter_lines_with_token(conflines, "@remove-line-for-nolocal@"); #else conflines = replace_token(conflines, "@remove-line-for-nolocal@", ""); #endif #ifdef HAVE_IPV6 /* * Probe to see if there is really any platform support for IPv6, and * comment out the relevant pg_hba line if not. This avoids runtime * warnings if getaddrinfo doesn't actually cope with IPv6. Particularly * useful on Windows, where executables built on a machine with IPv6 may * have to run on a machine without. */ { struct addrinfo *gai_result; struct addrinfo hints; int err = 0; #ifdef WIN32 /* need to call WSAStartup before calling getaddrinfo */ WSADATA wsaData; err = WSAStartup(MAKEWORD(2, 2), &wsaData); #endif /* for best results, this code should match parse_hba() */ hints.ai_flags = AI_NUMERICHOST; hints.ai_family = PF_UNSPEC; hints.ai_socktype = 0; hints.ai_protocol = 0; hints.ai_addrlen = 0; hints.ai_canonname = NULL; hints.ai_addr = NULL; hints.ai_next = NULL; if (err != 0 || getaddrinfo("::1", NULL, &hints, &gai_result) != 0) conflines = replace_token(conflines, "host all all ::1", "#host all all ::1"); } #else /* !HAVE_IPV6 */ /* If we didn't compile IPV6 support at all, always comment it out */ conflines = replace_token(conflines, "host all all ::1", "#host all all ::1"); #endif /* HAVE_IPV6 */ /* Replace default authentication methods */ conflines = replace_token(conflines, "@authmethod@", authmethod); conflines = replace_token(conflines, "@authmethodlocal@", authmethodlocal); conflines = replace_token(conflines, "@authcomment@", strcmp(authmethod, "trust") ? "" : AUTHTRUST_WARNING); /* Replace username for replication */ conflines = replace_token(conflines, "@default_username@", username); snprintf(path, sizeof(path), "%s/pg_hba.conf", pg_data); writefile(path, conflines); chmod(path, S_IRUSR | S_IWUSR); free(conflines); /* pg_ident.conf */ conflines = readfile(ident_file); snprintf(path, sizeof(path), "%s/pg_ident.conf", pg_data); writefile(path, conflines); chmod(path, S_IRUSR | S_IWUSR); free(conflines); check_ok(); }
相关文章
- PostgreSQL 如何计算两个时间点之间正常的工作日时间
- 10个常见的 PostgreSQL 错误及避坑指南
- PostgreSQL (慢SQL|数据库整体变慢|性能抖动) 数据库性能分析与优化方法
- 如何将数据从SQL Server 迁移到PostgreSQL?将数据从SQL Server 迁移到PostgreSQL方法分析!
- PostgreSQL远程连接,发生致命错误:没有用于主机“…”,用户“…”,数据库“…”,SSL关闭的pg_hba.conf记录
- 如何用 sysbench 并行装载 PostgreSQL 测试数据
- PostgreSQL 可靠性和一致性 代码分析
- PostgreSQL IDE pgadmin , edb postgres enterprise manager 查询慢的问题分析
- postgresql start shutdown
- 如何使用PostgreSQL中的JSONB数据类型(一)
- 数据库内核月报 - 2015 / 10-PgSQL · 特性分析 · PostgreSQL Aurora方案与DEMO
- 用icc编译Postgresql
- PostgreSQL的"天气预报" - 如何预测Freeze IO风暴
- PostgreSQL扫盲教程
- 如何在Kubernetes里给PostgreSQL创建secret
- mongoDB BI 分析利器 - PostgreSQL FDW (MongoDB Connector for BI)
- 技术实践丨PostgreSQL开启Huge Page场景分析
- PostgreSQL的学习心得和知识总结(一百一十二)|语法级自上而下完美实现PostgreSQL数据库的 EXCHANGE PARTION 的实现方案
- gitlab-ce实现主备切换集群:rsync+PostgreSQL备份的方式实现快速切换server ip实现伪高可用