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
相关文章
相关标签/搜索