linux 安裝postgresql

經過yum 安裝postgresql 相關的包

sudo yum install -y postgresql postgresql-server postgresql96-devel postgresql-contrib postgresql-docs
  • 初始化數據庫sql

    sudo service postgresql initdb
     // 根據安裝的版本肯定postgresql 的版本
     eg: sudo service postgresql96 initdb
  • 啓動數據庫數據庫

    sudo service postgresql start
  • 建立用戶和數據vim

    // 首先登錄postgres 用戶
    sudo su postpres psql
    
    // 輸入上條命令以後 進入psql ,就能夠輸入sql 語句
    
    create user testuser with password 'testpwd';
    
    // 建立數據庫
    
    create database testdb owner testuser;
     
     // 授予用戶操做數據庫的權限
     
     grant all privileges on database testdb to testuser;
     
     \q 退出
     
     // 修改配置文件,設置數據能夠遠程訪問
     
     sudo cd /var/lib/pgsql/data
     
     // 編輯文件
     
     sudo vim postgresql.conf
     
    修改文件
     listen_addresses = 'localhost' 改成  listen_addresses = '*'
     
     修改文件pg_hba.conf
     
    在文件底部添加一行
    host   all     all     0.0.0.0/0  md5
     // 重啓服務
     
     注意: 查看一下data文件的權限是不是 700 ,若是不是修改成700 sudo chmod 700 /var/bin/data/
     
     sudo service restart postgresql
    • 測試鏈接
    psql -h *.*.*.* -d testdb -U testuser

可能遇到的錯誤

Peer authentication failed for user "postgres"

解決方法

sudo vim /var/lib/pgsql/data/pg_hba.conf

host all all peer 改成

host all all trust
相關文章
相關標籤/搜索