简要
最近工作中与数据库打交道越来越多,操作数据频繁,感觉到了许多mysql操作的不便利性。想起了几年前使用的 postgreSQL
, 重新整理安装下,记录备查。
关于mysql
和 postgreSQL
的对比可见知乎的这个问题PostgreSQL 与 MySQL 相比,优势何在?。
看了下postgreSQL的发行版本,除了核心版本外还有针对大数据分析和虚拟化技术的分支版本,发展紧跟时代步伐,详见官方这里。对于postgreSQL的安装官方提供了许多方法,支持yum 安装、rpm安装、源码编译安装等方式。我这里采用yum 源安装,其他方式可参考官方文档。安装步骤如下:
yum源地址
https://yum.postgresql.org/repopackages.php#pg94
准备
检查系统是否安装了postgresSQL。若安装了需要卸载,清理干净,防止造成安装时不必要的问题。
[root@192 ~]# rpm -qa | grep postgres 检查是否已安装 [root@192 ~]# rpm -qal | grep postgres 检查安装位置 /usr/lib/firewalld/services/postgresql.xml /etc/selinux/targeted/active/modules/100/postgresql /etc/selinux/targeted/active/modules/100/postgresql/cil /etc/selinux/targeted/active/modules/100/postgresql/hll /etc/selinux/targeted/active/modules/100/postgresql/lang_ext /usr/lib64/security/pam_postgresok.so /usr/share/doc/pam-1.1.8/txts/README.pam_postgresok /usr/share/man/man8/pam_postgresok.8.gz
卸载
rpm -e postgresql94-contrib-9.4.4-1PGDG.rhel6.x86_64 postgresql94-server-9.4.4-1PGDG.rhel6.x86_64 rpm -e postgresql94-libs-9.4.4-1PGDG.rhel6.x86_64 postgresql94-9.4.4-1PGDG.rhel6.x86_64
安装yum源
[vip]
在官方文档选择自己系统对应的参数,获取到yum源的正确连接,执行安装
[root@192 ~]# yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
安装客户端和服务端
[root@192 ~]# yum install postgresql10 -y [root@192 ~]# yum install postgresql10-server -y
初始化数据库,启动服务
[root@192 ~]# /usr/pgsql-10/bin/postgresql-10-setup initdb Initializing database ... OK [root@192 ~]# systemctl enable postgresql-10 Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-10.service to /usr/lib/systemd/system/postgresql-10.service. [root@192 ~]# systemctl start postgresql-10
登录postgresql并设置密码
[root@192 ~]# su - postgres 上一次登录:三 7月 4 19:28:27 CST 2018pts/0 上 -bash-4.2$ psql psql (10.4) 输入 "help" 来获取帮助信息. postgres=# ALTER USER postgres WITH PASSWORD '123456'; ALTER ROLE postgres=# \q
默认情况下postgresql是不用密码不支持远程登录的。我们需要修改配置文件
[root@192 ~]# vi /var/lib/pgsql/10/data/pg_hba.conf # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all peer # IPv4 local connections: host all all 127.0.0.1/32 ident # IPv6 local connections: host all all ::1/128 ident # Allow replication connections from localhost, by a user with the # replication privilege. local replication all peer host replication all 127.0.0.1/32 ident host replication all ::1/128 ident
改为
# "local" is for Unix domain socket connections only local all all md5 # IPv4 local connections: host all all 0.0.0.0/0 md5 # IPv6 local connections: host all all ::1/128 md5 # Allow replication connections from localhost, by a user with the # replication privilege. #local replication all peer #host replication all 127.0.0.1/32 ident #host replication all ::1/128 ident host all all 0.0.0.0/0 md5
保存退出
我们改远程访问
[root@192 ~]# vim /var/lib/pgsql/10/data/postgresql.conf #listen_addresses = 'localhost' # what IP address(es) to listen on; # comma-separated list of addresses; # defaults to 'localhost'; use '*' for all # (change requires restart) #port = 5432 # (change requires restart) max_connections = 100 # (change requires restart)
改为
listen_addresses = '*' # what IP address(es) to listen on; # comma-separated list of addresses; # defaults to 'localhost'; use '*' for all # (change requires restart) port = 5432 # (change requires restart) max_connections = 100 # (change requires restart)
重启postgresql
[root@192 ~]# systemctl restart postgresql-10
机器里面登陆
[root@192 ~]# su - postgres 上一次登录:三 7月 4 19:36:39 CST 2018pts/1 上 -bash-4.2$ psql 口令: psql (10.4) 输入 "help" 来获取帮助信息. postgres-# \q
远程登陆验证
我们用pgadmin进行验证连接
我们测试创建数据库及创建用户
因为postgres属于superuser,我们需要创建部分低权限用户
创建数据库
-bash-4.2$ psql 口令: psql (10.4) 输入 "help" 来获取帮助信息. postgres=# CREATE DATABASE testdb; CREATE DATABASE
创建用户
postgres=# CREATE USER testuser CREATEDB LOGIN PASSWORD 'testpassword'; CREATE ROLE
将testdb所有权限赋给用户testuser
postgres=# GRANT ALL ON DATABASE testdb TO testuser; GRANT
查看现有数据库及用户
postgres=# \l 数据库列表 名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限 -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres testdb | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =Tc/postgres + | | | | | postgres=CTc/postgres+ | | | | | testuser=CTc/postgres (4 行记录) postgres=# \du 角色列表 角色名称 | 属性 | 成员属于 ----------+--------------------------------------------+---------- postgres | 超级用户, 建立角色, 建立 DB, 复制, 绕过RLS | {} testuser | 建立 DB | {}
删除数据库及测试用户
postgres=# drop database testdb; DROP DATABASE postgres=# drop role testuser; DROP ROLE postgres=# \l 数据库列表 名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限 -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 行记录) postgres=# \du 角色列表 角色名称 | 属性 | 成员属于 ----------+--------------------------------------------+---------- postgres | 超级用户, 建立角色, 建立 DB, 复制, 绕过RLS | {}
[/vip]
继续阅读
- 我的QQ
- QQ扫一扫
-
- 我的头条
- 头条扫一扫
-
评论