Postgres的搭建配置和创建

来源:互联网 发布:自动关注软件源码 编辑:程序博客网 时间:2024/05/29 08:48

由于项目需求,最近学习了postgres的相关知识。特别列下来作为记录。

软件环境: CentOS7 + Postgres 9.5

一  Postgres在线下载

     yum install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-2.noarch.rpm

二  Postgres的搭建配置

1.安装PostgreSQL 9.5
    yum install postgresql95-server postgresql95-contrib

2.初始化数据库
    /usr/pgsql-9.5/bin/postgresql95-setup initdb


3.设置开机自启动
    systemctl enable postgresql-9.5.service

4.启动服务
    systemctl start postgresql-9.5.service

安装完成!!

接下来,进行一下简单的配置。

系统管理用的帐号和密码变更完成,现在配置一下远程连接。(非常重要重要!!!)

6.开启远程访问
    vi /var/lib/pgsql/9.5/data/postgresql.conf
    修改#listen_addresses = 'localhost'  为  listen_addresses='*'   (此处‘*’也可以改为任何你想开放的服务器IP)


7.信任远程连接
    vi /var/lib/pgsql/9.5/data/pg_hba.conf
    修改如下内容,信任指定服务器连接
    # IPv4 local connections:
    host    all            all      127.0.0.1/32      trust
    host    all            all      10.211.55.6/32(需要连接的服务器IP)  trust


远程连接配置完成,由于系统原因,还需要在防火墙中打开相应的端口。

8.打开防火墙
    CentOS 防火墙中内置了PostgreSQL服务,配置文件位置在/usr/lib/firewalld/services/postgresql.xml,我们只需以服务方式将PostgreSQL服务开放即可。
    firewall-cmd --add-service=postgresql --permanent  开放postgresql服务
    firewall-cmd --reload  重载防火墙


9. 重启PostgreSQL数据服务,是上述设置生效(非常重要,不要忘记了!!!!)

    systemctl restart postgresql-9.5.service


备注:
作者:lasko
链接:http://www.jianshu.com/p/7e95fd0bc91a
來源:简书

三  创建新数据库

su - postgres  切换用户,执行后提示符会变为 '-bash-4.2$'

psql -U postgres 登录数据库,执行后提示符变为 'postgres=#'

postgres# CREATEUSER vcloud WITH PASSWORD 'vcloudpass';

 

postgres# CREATEDATABASE vcloud;

 

postgres# GRANT ALLPRIVILEGES ON DATABASE vcloud to vcloud;

 

psql-U vcloud  vcloud

\q  退出数据库

exit 


四  遇到的问题

issue 1: initdb data directory is not empty.

     

solution:  rm -rf /var/lib/pgsql/9.5/data    (Tips: 这是centos 6/7的解决办法)


postgres相关查看知识点:

psql testdb,系统登录用户,本地数据库,连接testdb数据库

psql -U testuser testdb,用户testuser,连接本地数据库testdb;

psql -U testuser -h 172.28.18.51 testdb,用户testuser,连接远程testdb数据库;

列举数据库:/l

选择数据库:/c  数据库名

显示表结构信息:/d 表名

退出psgl:/q

显示字符集:/encoding



原创粉丝点击