centos7 安装配置postgresql

考:https://www.linuxidc.com/Linux/2017-10/147536.htm

http://blog.51cto.com/12482328/2090844

https://www.cnblogs.com/think8848/p/5877076.html

主从配置:https://www.linuxidc.com/Linux/2017-03/142145.htm

一、系统环境

系统环境centos7.4

postgresql版本9.6.3

二、安装

1、安装rpm包

[html]  view plain  copy
 
  1. [root@www share]# yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm  

2、安装客户端

[html]  view plain  copy
 
  1. [root@www share]# yum install -y postgresql96  

3、安装服务器

[html]  view plain  copy
 
  1. [root@www share]# yum install -y postgresql96-server  
  2. 默认创建一个‘postgres’的系统账号,用于执行PostgreSQL;同时生成一个'postgres'的数据库;  

4、初始化

[html]  view plain  copy
 
  1. [root@www share]# /usr/pgsql-9.6/bin/postgresql96-setup initdb  

5、设置开机自启、启动

[html]  view plain  copy
 
  1. [root@www share]# systemctl enable postgresql-9.6  
  2. [root@www share]# systemctl start postgresql-9.6  

三、配置使用

1、修改用户密码

[html]  view plain  copy
 
  1. [root@www ~]# su postgres    //yum安装的默认创建一个'postgres'用户  
  2. bash-4.2$ psql -U postgres    //  进入postgres数据库  
  3. psql (9.6.9)  
  4. Type "help" for help.  
  5.   
  6. postgres=#                      
  7. postgres=# alter user postgres with password '密码'  

2、允许远程访问

[html]  view plain  copy
 
  1. [root@www ~]# find / -name postgresql.conf  
  2. /var/lib/pgsql/9.6/data/postgresql.conf  
  3. [root@www ~]# vi /var/lib/pgsql/9.6/data/postgresql.conf  
  4. // 修改listen_addresses = 'localhost'  改为 listen_addresses = '*'   需重启服务  

3、主机认证。

[html]  view plain  copy
 
  1. [root@www ~]# vim /var/lib/pgsql/9.6/data/pg_hba.conf  
  2. # IPv4 local connections:      //IPV4下面添加下面内容,第一个all是数据库,第二个是user,ip代表client ip,trust认证方法  
  3. host    all             all             127.0.0.1/32            ident  
  4. host    all             all             10.0.2.114/32           trust  

4、设置环境变量

[html]  view plain  copy
 
  1. [root@www ~]# vi /etc/profile  
  2. export PATH=$PATH:/usr/pgsql-9.6/bin  
  3. [root@www ~]# source /etc/profile  
  4. [root@www ~]# systemctl restart postgresql-9.6  

5. iptables

#postgresql默认开启tcp5432端口
[root@psql_master ~]# vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT

[root@psql_master ~]# service iptables restart

三、使用验证

1、查看端口

[html]  view plain  copy
 
  1. [root@www ~]# netstat -tunlp  
  2. Active Internet connections (only servers)  
  3. Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name  
  4. tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      870/nginx: master p  
  5. tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      29768/postmaster  

2、简单使用

[html]  view plain  copy
 
  1. [root@www ~]# su - postgres     // 切换用户  
  2. 上一次登录:一 5月 14 03:41:13 EDT 2018pts/1 上  
  3. -bash-4.2$  
  4. -bash-4.2$ psql -U postgres     // 进入数据库  
  5. psql (9.6.9)  
  6. Type "help" for help.  
  7.   
  8.   
  9. postgres=#  

2.1 创建用户

[html]  view plain  copy
 
  1. postgres=# create user user1 with password 'user123';  
  2. CREATE ROLE  

2.2 创建数据库

[html]  view plain  copy
 
  1. postgres=# create database t1 owner user1;  
  2. CREATE DATABASE  

2.3 数据库授权

[html]  view plain  copy
 
  1. postgres=# grant all privileges on database t1 to user1;  // 未授权只能登录控制台  

2.4 重新登录数据库

[html]  view plain  copy
 
  1. -bash-4.2$ psql -U user1 -d t1 -h 127.0.0.1 -p 5432  

2.5 创建表

postdb1=> create table tb1(
          id int primary key,
          name VARCHAR(20), 
          salary real
          );

2.6 插入数据

postdb1=> insert into tb1(
          id, name, salary)
          values(
          101, 'Mike', 5000.00
          );

2.7 查询

postdb1=>select * from tb1;

猜你喜欢

转载自www.cnblogs.com/sunshine-long/p/9059691.html