PostgreSQL 14.2在Linux平台上的源码编译安装与开机自启配置

作者:Nicky2024.01.22 15:02浏览量:32

简介:本文将指导您在Linux系统上从源码编译安装PostgreSQL 14.2,并配置其开机自启。我们将以Ubuntu为例,但这些步骤在其他Linux发行版上也应该适用。

首先,我们需要从PostgreSQL官方网站下载最新的源码包。在终端中输入以下命令:

  1. wget https://ftp.postgresql.org/pub/source/v14.2/postgresql-14.2.tar.gz

解压下载的源码包:

  1. tar -zxvf postgresql-14.2.tar.gz
  2. cd postgresql-14.2

接下来,我们需要配置编译选项。在postgresql-14.2目录下运行以下命令:

  1. ./configure --prefix=/usr/local/pgsql14.2 --with-pgport=5432 --with-python --with-openssl --with-libxml --with-libxslt --with-zlib --with-pam --with-gssapi --with-ldap --with-systemd

配置完成后,我们开始编译和安装:

  1. make world
  2. sudo make install

编译完成后,PostgreSQL将被安装到指定的/usr/local/pgsql14.2目录下。现在,我们需要初始化数据库集群。在终端中输入以下命令:

  1. /usr/local/pgsql14.2/bin/initdb -D /usr/local/pgsql14.2/data

初始化完成后,我们启动PostgreSQL服务:

  1. sudo /usr/local/pgsql14.2/bin/pg_ctl -D /usr/local/pgsql14.2/data -l logfile start

此时,PostgreSQL服务应该已经在后台运行了。您可以使用以下命令来检查服务状态:

  1. sudo /usr/local/pgsql14.2/bin/pg_ctl status -D /usr/local/pgsql14.2/data

接下来,我们将配置PostgreSQL开机自启。首先创建一个启动脚本:

  1. sudo nano /etc/systemd/system/postgresql@.service

在编辑器中输入以下内容:
```bash
[Unit]
Description=PostgreSQL Database Server for %i
After=syslog.target network.target munin.target nginx.target php7.3-fpm.target sshd.service postgresql@9.5-main.service mysql.service mongodb.service rabbitmq-server.service httpd.service memcached.service redis.service mongodb3.service mysql3.service postgresql@9.5-main3.service rabbitmq3.service httpd3.service memcached3.service redis3.service mongodb4.service mysql4.service postgresql@9.5-main4.service rabbitmq4.service httpd4.service memcached4.service redis4.service mongodb5.service mysql5.service postgresql@9.5-main5.service rabbitmq5.service httpd5.service memcached5.service redis5.service mongodb6.service mysql6.service postgresql@9.5-main6.service rabbitmq6.service httpd6.service memcached6.service redis6.service mongodb7.service mysql7.service postgresql@9.5-main7