温馨提示×

Linux PostgreSQL安装步骤是什么

小樊
40
2025-08-17 16:35:11
栏目: 云计算

Linux下安装PostgreSQL主要有在线和离线两种方式,以CentOS系统为例,具体步骤如下:

在线安装

  1. 添加仓库:使用命令sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm(CentOS 7)或sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm(CentOS 8)添加PostgreSQL官方仓库。
  2. 安装软件包:执行sudo yum install -y postgresql16-server(以PostgreSQL 16为例)安装数据库服务器。
  3. 初始化数据库:运行sudo /usr/pgsql-16/bin/postgresql-16-setup initdb初始化数据库。
  4. 启动服务:使用sudo systemctl enable postgresql-16sudo systemctl start postgresql-16设置开机自启并启动服务。

离线安装

  1. 准备安装包:下载PostgreSQL安装包及依赖包,如postgresql-15.1.tar.gz等,将其上传到服务器指定目录,如/usr/local
  2. 安装依赖:使用rpm -Uvh * --nodeps --force安装依赖包。
  3. 解压安装:解压安装包,如tar -zxvf postgresql-15.1.tar.gz,然后进入解压目录,执行./configure --prefix=/usr/local/postgresql --with-libxml,接着用gmakegmake install进行编译安装。
  4. 创建用户和目录:使用useradd postgres创建用户,再创建数据存储目录,如mkdir /usr/local/postgresql/data,并设置权限chown -R postgres:postgres /usr/local/postgresql/data
  5. 设置环境变量:编辑/etc/profile文件,添加export PGDATA=/usr/local/postgresql/data等环境变量,然后执行source /etc/profile使配置生效。
  6. 初始化数据库:切换到postgres用户,执行initdb命令初始化数据库。

0