Linux下安装PostgreSQL主要有在线和离线两种方式,以CentOS系统为例,具体步骤如下:
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官方仓库。sudo yum install -y postgresql16-server(以PostgreSQL 16为例)安装数据库服务器。sudo /usr/pgsql-16/bin/postgresql-16-setup initdb初始化数据库。sudo systemctl enable postgresql-16和sudo systemctl start postgresql-16设置开机自启并启动服务。postgresql-15.1.tar.gz等,将其上传到服务器指定目录,如/usr/local。rpm -Uvh * --nodeps --force安装依赖包。tar -zxvf postgresql-15.1.tar.gz,然后进入解压目录,执行./configure --prefix=/usr/local/postgresql --with-libxml,接着用gmake和gmake install进行编译安装。useradd postgres创建用户,再创建数据存储目录,如mkdir /usr/local/postgresql/data,并设置权限chown -R postgres:postgres /usr/local/postgresql/data。/etc/profile文件,添加export PGDATA=/usr/local/postgresql/data等环境变量,然后执行source /etc/profile使配置生效。postgres用户,执行initdb命令初始化数据库。