PostgreSQL 18 升级新体验:pg_upgrade --swap 极简教程

##

PostgreSQL 18 升级新体验:pg_upgrade --swap 极简教程

1、About pg_upgrade --swap

https://www.postgresql.org/about/news/postgresql-18-beta-1-released-3070/

Major version upgrade experience

Before PostgreSQL 18, an important step after performing a major version upgrade was to run the ANALYZE to generate statistics, which is a critical component of helping PostgreSQL to select the most efficient query plan. Based on the size and overall activity of a PostgreSQL cluster, this could be a time consuming process, and potentially impact query performance until the process completed. PostgreSQL 18 introduces the ability to keep planner statistics through a major version upgrade, which helps an upgraded cluster to get to its expected performance state sooner once it’s available.

Additionally, pg_upgrade, the utility used to facilitate a major version upgrade, added several performance enhancements to help accelerate upgrades with many objects, such as tables and sequences. This release also allows pg_upgrade to process its checks in parallel based on the settings of the --jobs flag, and also adds the --swap flag, which swaps upgrade directories instead of copying, cloning, or linking files.

https://www.postgresql.org/docs/18/pgupgrade.html

  • --swap

    Move the data directories from the old cluster to the new cluster. Then, replace the catalog files with those generated for the new cluster. This mode can outperform --link, --clone, --copy, and --copy-file-range, especially on clusters with many relations.However, this mode creates many garbage files in the old cluster, which can prolong the file synchronization step if --sync-method=syncfs is used. Therefore, it is recommended to use --sync-method=fsync with --swap.Additionally, once the file transfer step begins, the old cluster will be destructively modified and therefore will no longer be safe to start. See Step 17 for details.

2、Practical process
[root@pgdb01 ~]# su - postgres
[postgres@pgdb01:/home/postgres]$pg_ctl start
waiting for server to start....2025-05-22 10:09:02.517 CST [3694] LOG:  redirecting log output to logging collector process
2025-05-22 10:09:02.517 CST [3694] HINT:  Future log output will appear in directory "/postgresql/log".
 done
server started
[postgres@pgdb01:/home/postgres]$psql
psql (17.4)
Type "help" for help.

postgres=# \l+
                                                                                  List of databases
   Name    |  Owner   | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules |   Access privileges   |  Size   | Tablespace |                Description
-----------+----------+----------+-----------------+---------+-------+--------+-----------+-----------------------+---------+------------+--------------------------------------------
 demo      | postgres | UTF8     | libc            | C       | C     |        |           |                       | 281 MB  | pg_default |
 postgres  | postgres | UTF8     | libc            | C       | C     |        |           |                       | 7699 kB | pg_default | default administrative connection database
 template0 | postgres | UTF8     | libc            | C       | C     |        |           | =c/postgres          +| 7545 kB | pg_default | unmodifiable empty database
           |          |          |                 |         |       |        |           | postgres=CTc/postgres |         |            |
 template1 | postgres | UTF8     | libc            | C       | C     |        |           | =c/postgres          +| 7617 kB | pg_default | default template for new databases
           |          |          |                 |         |       |        |           | postgres=CTc/postgres |         |            |
(4 rows)

postgres=# \q
[postgres@pgdb01:/home/postgres]$psql --version
psql (PostgreSQL) 17.4

[postgres@pgdb01:/home/postgres]$psql
psql (17.4)
Type "help" for help.

postgres=# show server_version;
 server_version
----------------
 17.4
(1 row)

postgres=# \q
[postgres@pgdb01:/home/postgres]$exit
logout
[root@pgdb01 ~]# chown -R postgres:postgres /postgresql/backup/*
[root@pgdb01 ~]# su - postgres
[postgres@pgdb01:/home/postgres]$cd /postgresql/backup/
[postgres@pgdb01:/postgresql/backup]$ll
total 178904
-rwxrwxrwx  1 postgres postgres 103865532 Feb 21  2018 demo-small-en-20170815.sql
-rw-r--r--  1 postgres postgres  22187739 Mar 22 20:03 demo-small-en.zip
drwxr-xr-x  6 postgres postgres      4096 Feb 28 11:53 postgresql-17.4
-rw-r--r--  1 postgres postgres  28056622 Feb 28 11:50 postgresql-17.4.tar.gz
-rw-r--r--  1 postgres postgres  29048976 May 22 10:11 postgresql-18beta1.tar.gz
-rwxrwxr-x. 1 postgres postgres     24777 Jan 22  2024 uuid-devel-1.6.2-55.el9.x86_64.rpm
[postgres@pgdb01:/postgresql/backup]$tar -zxvf postgresql-18beta1.tar.gz

[postgres@pgdb01:/postgresql/backup]$cd postgresql-18beta1/
[postgres@pgdb01:/postgresql/backup/postgresql-18beta1]$ll
total 832
-rw-r--r--  1 postgres postgres    365 May  6 04:25 aclocal.m4
drwxr-xr-x  2 postgres postgres   4096 May  6 04:25 config
-rwxr-xr-x  1 postgres postgres 588907 May  6 04:25 configure
-rw-r--r--  1 postgres postgres  88909 May  6 04:25 configure.ac
drwxr-xr-x 61 postgres postgres   4096 May  6 04:25 contrib
-rw-r--r--  1 postgres postgres   1192 May  6 04:25 COPYRIGHT
drwxr-xr-x  3 postgres postgres     87 May  6 04:25 doc
-rw-r--r--  1 postgres postgres   4176 May  6 04:25 GNUmakefile.in
-rw-r--r--  1 postgres postgres    277 May  6 04:25 HISTORY
-rw-r--r--  1 postgres postgres   1825 May  6 04:25 Makefile
-rw-r--r--  1 postgres postgres 121002 May  6 04:25 meson.build
-rw-r--r--  1 postgres postgres   6641 May  6 04:25 meson_options.txt
-rw-r--r--  1 postgres postgres    989 May  6 04:25 README.md
drwxr-xr-x 16 postgres postgres   4096 May  6 04:25 src

[postgres@pgdb01:/postgresql/backup/postgresql-18beta1]$export PREFIX=/postgresql/app/pg180

[postgres@pgdb01:/postgresql/backup/postgresql-18beta1]$./configure --prefix=${PREFIX} --with-blocksize=8 --with-segsize=1 --with-wal-blocksize=8 --enable-nls="zh_CN en_US" --with-llvm LL                    VM_CONFIG=/usr/bin/llvm-config CLANG=/usr/bin/clang --with-icu --with-tcl --with-perl --with-python --with-gssapi --with-pam --with-ldap --with-readline --with-libedit-preferred --with-li                    bxml --with-libxslt --with-lz4 --with-zstd --with-gnu-ld --with-openssl --with-uuid=LIB --with-ossp-uuid --with-zlib --with-systemd


[postgres@pgdb01:/postgresql/backup/postgresql-18beta1]$make world -j 4
[postgres@pgdb01:/postgresql/backup/postgresql-18beta1]$make install-world -j 4

[postgres@pgdb01:/postgresql/backup/postgresql-18beta1]$cd /postgresql/app/pg180/
[postgres@pgdb01:/postgresql/app/pg180]$cd /postgresql/app/pg180/bin/

[postgres@pgdb01:/postgresql/app/pg180/bin]$./initdb -D /postgresql/data18 -E UTF8 --locale=C -U postgres -W
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".

Data page checksums are enabled.

Enter new superuser password:
Enter it again:

creating directory /postgresql/data18 ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default "max_connections" ... 100
selecting default "shared_buffers" ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /postgresql/data18 -l logfile start

[postgres@pgdb01:/postgresql/app/pg180/bin]$vi /postgresql/data18/postgresql.conf


[postgres@pgdb01:/postgresql/app/pg180/bin]$/postgresql/app/pg180/bin/pg_upgrade -b /postgresql/app/pg174/bin -B /postgresql/app/pg180/bin -d /postgresql/data/ -D /postgresql/data18 -                        p 5432 -P 5433 -c
Performing Consistency Checks on Old Live Server
------------------------------------------------
Checking cluster versions                                     ok

old cluster does not use data checksums but the new one does
Failure, exiting
[postgres@pgdb01:/postgresql/app/pg180/bin]$rm -fr /postgresql/data18/*
[postgres@pgdb01:/postgresql/app/pg180/bin]$./initdb --help
  -k, --data-checksums      use data page checksums

      --no-data-checksums   do n
* file[/var/log/gitlab/gitlab-kas/current] action touch (skipped due to only_if) Recipe: gitlab::database_migrations * ruby_block[check remote PG version] action nothing (skipped due to action :nothing) * rails_migration[gitlab-rails] action run[2025-07-14T15:04:58+08:00] WARN: gitlab-rails does not have a log_group or default logdir mode defined. Setting to 0700. * bash_hide_env[migrate gitlab-rails database] action run ================================================================================ Error executing action `run` on resource 'bash_hide_env[migrate gitlab-rails database]' ================================================================================ Mixlib::ShellOut::ShellCommandFailed ------------------------------------ Expected process to exit with [0], but received '137' ---- Begin output of "bash" ---- STDOUT: STDERR: ---- End output of "bash" ---- Ran "bash" returned 137 Cookbook Trace: (most recent call first) ---------------------------------------- /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/resources/rails_migration.rb:20:in `block in class_from_file' Resource Declaration: --------------------- # In /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/resources/rails_migration.rb 20: bash_hide_env "migrate #{new_resource.name} database" do 21: code <<-EOH 22: set -e 23: log_file="#{logging_settings[:log_directory]}/#{new_resource.logfile_prefix}-$(date +%Y-%m-%d-%H-%M-%S).log" 24: umask 077 25: /opt/gitlab/bin/gitlab-rake #{new_resource.rake_task} 2>& 1 | tee ${log_file} 26: STATUS=${PIPESTATUS[0]} 27: chown #{account_helper.gitlab_user}:#{account_helper.gitlab_group} ${log_file} 28: echo $STATUS > #{new_resource.helper.db_migrate_status_file} 29: exit $STATUS 30: EOH 31: 32: environment new_resource.environment if new_resource.property_is_set?(:environment) 33: new_resource.dependent_services.each do |svc| 34: notifies :restart, svc, :immediately 35: end 36: 37: not_if { new_resource.helper.migrated? } 38: sensitive false 39: end 40: end Compiled Resource: ------------------ # Declared in /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/resources/rails_migration.rb:20:in `block in class_from_file' bash_hide_env("migrate gitlab-rails database") do action [:run] default_guard_interpreter :default interpreter "bash" declared_type :bash_hide_env cookbook_name "gitlab" recipe_name "database_migrations" code " set -e\n log_file=\"/var/log/gitlab/gitlab-rails/gitlab-rails-db-migrate-$(date +%Y-%m-%d-%H-%M-%S).log\"\n umask 077\n /opt/gitlab/bin/gitlab-rake gitlab:db:configure 2>& 1 | tee ${log_file}\n STATUS=${PIPESTATUS[0]}\n chown git:git ${log_file}\n echo $STATUS > /var/opt/gitlab/gitlab-rails/upgrade-status/db-migrate-c6697a9cd49e1cfa45cc31b1ecdb90ae-bd824d1abb2\n exit $STATUS\n" environment "*sensitive value suppressed*" not_if { #code block } end System Info: ------------ chef_version=18.3.0 platform=centos platform_version=7.6.1810 ruby=ruby 3.1.5p253 (2024-04-023 revision 1945f8dc0e) [x86_64-linux] program_name=/opt/gitlab/embedded/bin/cinc-client executable=/opt/gitlab/embedded/bin/cinc-client ================================================================================ Error executing action `run` on resource 'rails_migration[gitlab-rails]' ================================================================================ Mixlib::ShellOut::ShellCommandFailed ------------------------------------ bash_hide_env[migrate gitlab-rails database] (gitlab::database_migrations line 20) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '137' ---- Begin output of "bash" ---- STDOUT: STDERR: ---- End output of "bash" ---- Ran "bash" returned 137 Cookbook Trace: (most recent call first) ---------------------------------------- /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/resources/rails_migration.rb:20:in `block in class_from_file' Resource Declaration: --------------------- # In /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/recipes/database_migrations.rb 51: rails_migration "gitlab-rails" do 52: rake_task 'gitlab:db:configure' 53: logfile_prefix 'gitlab-rails-db-migrate' 54: helper migration_helper 55: 56: environment env_variables 57: dependent_services dependent_services 58: notifies :run, "execute[clear the gitlab-rails cache]", :immediately 59: notifies :run, "ruby_block[check remote PG version]", :immediately 60: 61: only_if { migration_helper.attributes_node['auto_migrate'] } 62: end Compiled Resource: ------------------ # Declared in /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/recipes/database_migrations.rb:51:in `from_file' rails_migration("gitlab-rails") do action [:run] default_guard_interpreter :default declared_type :rails_migration cookbook_name "gitlab" recipe_name "database_migrations" rake_task "gitlab:db:configure" logfile_prefix "gitlab-rails-db-migrate" helper "*sensitive value suppressed*" environment "*sensitive value suppressed*" only_if { #code block } end System Info: ------------ chef_version=18.3.0 platform=centos platform_version=7.6.1810 ruby=ruby 3.1.5p253 (2024-04-023 revision 1945f8dc0e) [x86_64-linux] program_name=/opt/gitlab/embedded/bin/cinc-client executable=/opt/gitlab/embedded/bin/cinc-client [2025-07-14T15:05:48+08:00] INFO: Running queued delayed notifications before re-raising exception [2025-07-14T15:05:48+08:00] INFO: templatesymlink[Create a gitlab.yml and create a symlink to Rails root] sending run action to execute[clear the gitlab-rails cache] (delayed) Recipe: gitlab::gitlab-rails * execute[clear the gitlab-rails cache] action run ================================================================================ Error executing action `run` on resource 'execute[clear the gitlab-rails cache]' ================================================================================ Mixlib::ShellOut::ShellCommandFailed ------------------------------------ Expected process to exit with [0], but received '' ---- Begin output of /opt/gitlab/bin/gitlab-rake cache:clear ---- STDOUT: STDERR: ---- End output of /opt/gitlab/bin/gitlab-rake cache:clear ---- Ran /opt/gitlab/bin/gitlab-rake cache:clear returned Resource Declaration: --------------------- # In /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/recipes/gitlab-rails.rb 543: execute "clear the gitlab-rails cache" do 544: command "/opt/gitlab/bin/gitlab-rake cache:clear" 545: action :nothing 546: not_if { omnibus_helper.not_listening?('redis') || !node['gitlab']['gitlab_rails']['rake_cache_clear'] } 547: end 548: Compiled Resource: ------------------ # Declared in /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/recipes/gitlab-rails.rb:543:in `from_file' execute("clear the gitlab-rails cache") do action [:nothing] default_guard_interpreter :execute command "/opt/gitlab/bin/gitlab-rake cache:clear" declared_type :execute cookbook_name "gitlab" recipe_name "gitlab-rails" not_if { #code block } end System Info: ------------ chef_version=18.3.0 platform=centos platform_version=7.6.1810 ruby=ruby 3.1.5p253 (2024-04-023 revision 1945f8dc0e) [x86_64-linux] program_name=/opt/gitlab/embedded/bin/cinc-client executable=/opt/gitlab/embedded/bin/cinc-client [2025-07-14T15:06:56+08:00] INFO: version_file[Create version file for Gitaly] sending hup action to runit_service[gitaly] (delayed) Recipe: gitaly::enable * runit_service[gitaly] action hup[2025-07-14T15:06:56+08:00] INFO: runit_service[gitaly] signalled (HUP) [2025-07-14T15:06:56+08:00] INFO: runit_service[gitaly] sent hup - send hup to runit_service[gitaly] [2025-07-14T15:06:56+08:00] INFO: file[create /opt/gitlab/embedded/etc/90-omnibus-gitlab-kernel.shmmax.conf kernel.shmmax] sending run action to execute[reload all sysctl conf] (delayed) Recipe: package::sysctl * execute[reload all sysctl conf] action run [execute] * Applying /usr/lib/sysctl.d/00-system.conf ... net.bridge.bridge-nf-call-ip6tables = 0 net.bridge.bridge-nf-call-iptables = 0 net.bridge.bridge-nf-call-arptables = 0 * Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ... kernel.yama.ptrace_scope = 0 * Applying /usr/lib/sysctl.d/50-default.conf ... kernel.sysrq = 16 kernel.core_uses_pid = 1 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 net.ipv4.conf.all.accept_source_route = 0 net.ipv4.conf.default.promote_secondaries = 1 net.ipv4.conf.all.promote_secondaries = 1 fs.protected_hardlinks = 1 fs.protected_symlinks = 1 * Applying /etc/sysctl.d/90-omnibus-gitlab-kernel.sem.conf ... kernel.sem = 250 32000 32 275 * Applying /etc/sysctl.d/90-omnibus-gitlab-kernel.shmall.conf ... kernel.shmall = 4194304 * Applying /etc/sysctl.d/90-omnibus-gitlab-kernel.shmmax.conf ... kernel.shmmax = 17179869184 * Applying /etc/sysctl.d/99-sysctl.conf ... net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-arptables = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_local_reserved_ports = 30000-32767 vm.max_map_count = 262144 vm.swappiness = 1 fs.inotify.max_user_instances = 524288 kernel.pid_max = 65535 vm.overcommit_memory = 1 net.core.somaxconn = 511 * Applying /etc/sysctl.d/lvs_dr.conf ... net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.lo.arp_announce = 2 * Applying /etc/sysctl.conf ... net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-arptables = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_local_reserved_ports = 30000-32767 vm.max_map_count = 262144 vm.swappiness = 1 fs.inotify.max_user_instances = 524288 kernel.pid_max = 65535 vm.overcommit_memory = 1 net.core.somaxconn = 511 [2025-07-14T15:06:56+08:00] INFO: execute[reload all sysctl conf] ran successfully - execute sysctl -e --system Running handlers: [2025-07-14T15:06:56+08:00] ERROR: Running exception handlers There was an error running gitlab-ctl reconfigure: Multiple failures occurred: * Mixlib::ShellOut::ShellCommandFailed occurred in Cinc Client run: rails_migration[gitlab-rails] (gitlab::database_migrations line 51) had an error: Mixlib::ShellOut::ShellCommandFailed: bash_hide_env[migrate gitlab-rails database] (gitlab::database_migrations line 20) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '137' ---- Begin output of "bash" ---- STDOUT: STDERR: ---- End output of "bash" ---- Ran "bash" returned 137 * Mixlib::ShellOut::ShellCommandFailed occurred in delayed notification: execute[clear the gitlab-rails cache] (gitlab::gitlab-rails line 543) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '' ---- Begin output of /opt/gitlab/bin/gitlab-rake cache:clear ---- STDOUT: STDERR: ---- End output of /opt/gitlab/bin/gitlab-rake cache:clear ---- Ran /opt/gitlab/bin/gitlab-rake cache:clear returned Notes: Default admin account has been configured with following details: Username: root Password: You didn't opt-in to print initial root password to STDOUT. Password stored to /etc/gitlab/initial_root_password. This file will be cleaned up in first reconfigure run after 24 hours. NOTE: Because these credentials might be present in your log files in plain text, it is highly recommended to reset the password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password. Running handlers complete [2025-07-14T15:06:56+08:00] ERROR: Exception handlers complete Infra Phase failed. 319 resources updated in 02 minutes 41 seconds Notes: Default admin account has been configured with following details: Username: root Password: You didn't opt-in to print initial root password to STDOUT. Password stored to /etc/gitlab/initial_root_password. This file will be cleaned up in first reconfigure run after 24 hours. NOTE: Because these credentials might be present in your log files in plain text, it is highly recommended to reset the password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password. [2025-07-14T15:06:56+08:00] FATAL: Stacktrace dumped to /opt/gitlab/embedded/cookbooks/cache/cinc-stacktrace.out [2025-07-14T15:06:56+08:00] FATAL: --------------------------------------------------------------------------------------- [2025-07-14T15:06:56+08:00] FATAL: PLEASE PROVIDE THE CONTENTS OF THE stacktrace.out FILE (above) IF YOU FILE A BUG REPORT [2025-07-14T15:06:56+08:00] FATAL: --------------------------------------------------------------------------------------- [2025-07-14T15:06:56+08:00] FATAL: Chef::Exceptions::MultipleFailures: Multiple failures occurred: * Mixlib::ShellOut::ShellCommandFailed occurred in Cinc Client run: rails_migration[gitlab-rails] (gitlab::database_migrations line 51) had an error: Mixlib::ShellOut::ShellCommandFailed: bash_hide_env[migrate gitlab-rails database] (gitlab::database_migrations line 20) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '137' ---- Begin output of "bash" ---- STDOUT: STDERR: ---- End output of "bash" ---- Ran "bash" returned 137 * Mixlib::ShellOut::ShellCommandFailed occurred in delayed notification: execute[clear the gitlab-rails cache] (gitlab::gitlab-rails line 543) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '' ---- Begin output of /opt/gitlab/bin/gitlab-rake cache:clear ---- STDOUT: STDERR: ---- End output of /opt/gitlab/bin/gitlab-rake cache:clear ---- Ran /opt/gitlab/bin/gitlab-rake cache:clear returned
07-15
<think>我们正在解决用户的问题:用户可能尝试运行`pg_config --version`命令时遇到`command not found`错误,需要找到`pg_config`或验证PostgreSQL安装。 根据引用[1][3][4]可知,`pg_config`命令属于PostgreSQL的开发包,通常由`libpq-dev`或类似名称的包提供。 因此,我们需要指导用户如何安装包含`pg_config`的包,并验证安装。 步骤: 1. 检查系统是否已安装`pg_config`(可能不在PATH中) 2. 如果没有找到,则安装包含`pg_config`的包(根据不同的Linux发行版) 3. 验证安装 注意:引用中提到了Ubuntu和CentOS,所以我们需要分别说明。</think>要解决`pg_config --version`报错"command not found"的问题,请按以下步骤操作: ### 一、定位pg_config缺失原因 1. **检查基础安装状态** ```bash # 验证PostgreSQL服务是否安装 systemctl status postgresql ``` - 若服务不存在,说明PostgreSQL未安装(需执行步骤二) - 若服务存在但报错,说明缺少开发包(需执行步骤三) 2. **搜索pg_config位置** ```bash # 全盘搜索pg_config文件 sudo find / -name pg_config 2>/dev/null ``` - 若找到路径如`/usr/lib/postgresql/16/bin/pg_config`,将其加入PATH: `export PATH=$PATH:/usr/lib/postgresql/16/bin` ### 二、安装PostgreSQL(若未安装) ```bash # Ubuntu/Debian sudo apt update sudo apt install postgresql postgresql-contrib # 基础服务[^1] # CentOS/RHEL sudo yum install postgresql-server postgresql-contrib sudo postgresql-setup --initdb sudo systemctl start postgresql ``` ### 三、安装开发包(关键步骤) ```bash # Ubuntu/Debian(安装libpq-dev) sudo apt install libpq-dev # 包含pg_config[^1][^3] # CentOS/RHEL(安装开发包) sudo yum install postgresql-devel # 提供pg_config ``` ### 四、验证安装结果 ```bash # 检查pg_config可用性 pg_config --version # 应输出类似"PostgreSQL 16.1" # 检查开发头文件位置 pg_config --includedir # 应返回路径如/usr/include/postgresql ``` > **重要提示**: > - Python环境安装`psycopg2`时若报错`Error: pg_config executable not found`[^3][^4],必须完成**步骤三**安装开发包 > - 源码编译PostgreSQL扩展(如pgvector)时,需确保`pg_config`在PATH中[^2] ### 五、环境配置(可选) ```bash # 永久添加pg_config到PATH echo 'export PATH=$PATH:$(pg_config --bindir)' >> ~/.bashrc source ~/.bashrc ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值