linux

Linux下如何安装PostgreSQL

小樊
54
2025-08-07 12:28:42
栏目: 云计算

在Linux下安装PostgreSQL,可以按照以下步骤进行:

方法一:使用包管理器(推荐)

1. 更新系统包列表

sudo apt update

2. 安装PostgreSQL

对于基于Debian的系统(如Ubuntu):

sudo apt install postgresql postgresql-contrib

对于基于Red Hat的系统(如CentOS、Fedora):

sudo yum install postgresql-server postgresql-contrib

或者使用dnf(Fedora 22+):

sudo dnf install postgresql-server postgresql-contrib

3. 启动并启用PostgreSQL服务

sudo systemctl start postgresql
sudo systemctl enable postgresql

4. 验证安装

检查PostgreSQL服务状态:

sudo systemctl status postgresql

方法二:使用源码编译安装

1. 安装依赖包

sudo apt-get update
sudo apt-get install build-essential libpq-dev

2. 下载PostgreSQL源码

访问PostgreSQL官方网站,下载最新版本的源码包,例如:

wget https://ftp.postgresql.org/pub/source/v14.3/postgresql-14.3.tar.gz

3. 解压源码包

tar -zxvf postgresql-14.3.tar.gz
cd postgresql-14.3

4. 配置编译选项

./configure --prefix=/usr/local/pgsql --with-libpq=/usr/lib/x86_64-linux-gnu --with-openssl=/usr/bin/openssl

5. 编译并安装

make
sudo make install

6. 初始化数据库集群

sudo mkdir /usr/local/pgsql/data
sudo chown $USER /usr/local/pgsql/data
cd /usr/local/pgsql/bin
./initdb -D /usr/local/pgsql/data

7. 启动PostgreSQL服务

./pg_ctl -D /usr/local/pgsql/data start

8. 设置环境变量

将PostgreSQL的bin目录添加到PATH中:

echo 'export PATH=$PATH:/usr/local/pgsql/bin' >> ~/.bashrc
source ~/.bashrc

9. 验证安装

检查PostgreSQL服务状态:

pg_ctl -D /usr/local/pgsql/data status

注意事项

通过以上步骤,你应该能够在Linux系统上成功安装PostgreSQL。

0
看了该问题的人还看了