本文简单讲解了postgre的安装,以及如何设置远程访问,以及用户及表的创建和postgres的简单操作。
1. 安装postgre
1.1 下载安装
yum install http://yum.postgresql.org/9.5/redhat/rhel-7-x86_64/pgdg-redhat95-9.5-2.noarch.rpm -y
#安装postgresql服务
yum install postgresql95-server postgresql95-contrib -y
1.2 设定目录
#创建目录
mkdir -p /data/postgresql_data
#设定权限
chown postgres:postgres /data/postgresql_data
chmod 750 /data/postgresql_data
1.3 设定环境变量
export PG_SQL_HOME=/usr/pgsql-9.5
export LD_LIBRARY_PATH=/usr/pgsql-9.5/lib
export PGDATA=/data/postgresql_data
export PATH=$PATH:$PG_SQL_HOME/bin
1.4 初始化数据库
#切换到 postgres 用户
su - postgres
export PGDATA=/data/postgresql_data
#初始化数据库
initdb
# 启动postgres数据库
pg_ctl start -D $PGDATA
先设定postgresql的数据存储目录,先暴漏一个环境变量,然后设定服务。
启动数据库
1.5 配置service
vi /usr/lib/systemd/system/postgresql-9.5.service
#添加存储的地址
Environment=PGDATA=/data/postgresql_data
1.6 重启服务
#干掉之前启动的实例,不然就会导致服务重复启动不起来
kill -9 $(ps -ef|grep post |awk '{print $2}')
#设置开机启动
systemctl enable postgresql-9.5.service
#启动服务
service postgresql-9.5 start
#查看服务状态
service postgresql-9.5 status
2 开启远程访问
2.1 配置postgresql.conf
查看postgresql的数据目录,然后配置postgresql.conf 文件
配置远程访问。
#设定监听得地址
listen_addresses = '*'
#设定端口
port = 5432
#设定加密
password_encryption = on
2.2 配置pg_hba.conf
配置设定验证方式,以及设定所有ip都可以链接
host all all 0.0.0.0/0 md5
重启服务后,配置生效
#重启post服务
service postgresql-9.5 restart
#查看post信息
ps -ef|grep post
#查看端口号信息
netstat -nptl
3 创建数据库
3.1 登陆数据库
然后使用psql工具登录数据库,列出当前的数据库,命令分别是 psql 和 \l
#创建数据库
create database yellowcong;
#创建数据库的用户
CREATE USER huangcong CREATEDB LOGIN PASSWORD 'admin';
#数据库设置授权
GRANT ALL ON DATABASE yellowcong TO huangcong ;
#退出服务
\q
3.2 用户远程登陆
psql -U huangcong -h 127.0.0.1 -p 5432 -d yellowcong -W
#切换数据库
\c postgres
#查看当前的表信息
\d
可以看到远程链接成功
创建了一个简单的表,然后通过\d来查看当前的表信息。