温馨提示×

Linux下PostgreSQL安装步骤是什么

小樊
39
2025-09-08 19:16:19
栏目: 云计算

Linux下安装PostgreSQL主要有以下两种常见方法:

基于包管理器安装(以Ubuntu和CentOS为例)

  • Ubuntu系统
    1. 更新软件包列表:sudo apt update
    2. 安装PostgreSQL:sudo apt install postgresql postgresql-contrib
  • CentOS系统
    1. 添加PostgreSQL官方仓库:sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %{rhel})-x86_64/pgdg-redhat-repo-latest.noarch.rpm
    2. 安装PostgreSQL:sudo yum install -y postgresql15-server(版本号可按需修改)。
    3. 初始化数据库:sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
    4. 启动服务并设置开机自启:sudo systemctl enable postgresql-15sudo systemctl start postgresql-15

源码编译安装:

  1. 安装依赖包,如readline-develzlib-develgcc-c++等。
  2. 下载PostgreSQL源码包并解压。
  3. 配置编译选项,如指定安装路径./configure --prefix=/usr/local/pgsql
  4. 编译并安装:makesudo make install
  5. 创建PostgreSQL用户和数据目录,设置权限。
  6. 初始化数据库:sudo -u postgres /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
  7. 配置环境变量(可选),将PostgreSQL路径添加到~/.bashrc/etc/profile中。

0