温馨提示×

CentOS上如何安装PostgreSQL

小樊
91
2025-02-14 16:46:51
栏目: 云计算
Centos服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在CentOS上安装PostgreSQL的步骤如下:

方法一:使用yum安装

  1. 更新系统包

    sudo yum update -y
    
  2. 启用PostgreSQL官方仓库

    sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
    
  3. 安装PostgreSQL

    sudo yum install -y postgresql13-server
    
  4. 初始化数据库

    sudo postgresql-setup --initdb
    
  5. 启动PostgreSQL服务

    sudo systemctl start postgresql
    
  6. 设置开机自启

    sudo systemctl enable postgresql
    
  7. 创建数据库用户和数据库

    sudo -u postgres psql
    

    在psql命令行中:

    CREATE USER your_username WITH PASSWORD 'your_password';
    CREATE DATABASE your_database OWNER your_username;
    GRANT ALL PRIVILEGES ON DATABASE your_database TO your_username;
    \q
    

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

  1. 安装依赖包

    sudo yum groupinstall -y "Development Tools"
    sudo yum install -y readline-devel zlib-devel
    
  2. 下载PostgreSQL源码

    wget https://ftp.postgresql.org/pub/source/v13.6/postgresql-13.6.tar.gz
    tar -zxvf postgresql-13.6.tar.gz
    cd postgresql-13.6
    
  3. 配置编译选项

    ./configure --prefix=/usr/local/pgsql --with-includes=/usr/include --with-libraries=/usr/lib64
    
  4. 编译并安装

    make
    sudo make install
    
  5. 初始化数据库

    /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
    
  6. 启动PostgreSQL服务

    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data start
    
  7. 设置开机自启(可选) 可以创建一个systemd服务文件来实现开机自启。

验证安装

无论使用哪种方法安装,都可以通过以下命令验证PostgreSQL是否安装成功:

psql --version

如果显示了PostgreSQL的版本信息,则表示安装成功。

注意事项

  • 在生产环境中,建议使用SSL加密连接。
  • 定期备份数据库以防数据丢失。
  • 根据实际需求调整PostgreSQL的配置文件postgresql.confpg_hba.conf

希望这些步骤能帮助你在CentOS上成功安装PostgreSQL!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:在 CentOS 上安装 PGAdmin 后如何连接 PostgreSQL

0