在CentOS系统上安装PostgreSQL数据库,可以按照以下步骤进行:
更新系统包
sudo yum update -y 启用PostgreSQL仓库
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm 安装PostgreSQL
sudo yum install -y postgresql12-server 这里以安装PostgreSQL 12为例,你可以根据需要选择其他版本。
初始化数据库集群
sudo postgresql-setup --initdb 启动PostgreSQL服务
sudo systemctl start postgresql 设置开机自启
sudo systemctl enable postgresql 创建数据库和用户
sudo -u postgres psql CREATE DATABASE mydatabase; CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypassword'; GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser; 退出psql
\q 如果你需要安装特定版本的PostgreSQL或者有特殊需求,可以选择从源码编译安装。
安装依赖包
sudo yum groupinstall -y "Development Tools" sudo yum install -y readline-devel zlib-devel 下载PostgreSQL源码 访问PostgreSQL官方下载页面,选择合适的版本并下载。
wget https://ftp.postgresql.org/pub/source/v13.3/postgresql-13.3.tar.gz tar -zxvf postgresql-13.3.tar.gz cd postgresql-13.3 配置编译选项
./configure --prefix=/usr/local/pgsql --with-includes=/usr/include --with-libraries=/usr/lib64 编译并安装
make sudo make install 初始化数据库集群
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data 启动PostgreSQL服务
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data start 设置环境变量 编辑~/.bash_profile或~/.bashrc文件,添加以下内容:
export PATH=/usr/local/pgsql/bin:$PATH 然后执行:
source ~/.bash_profile 创建数据库和用户
/usr/local/pgsql/bin/psql -U postgres CREATE DATABASE mydatabase; CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypassword'; GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser; 退出psql
\q 通过以上步骤,你应该能够在CentOS系统上成功安装并配置PostgreSQL数据库。