更多请看(www.omob.cc)
我在自己一台老旧的DELL电脑上安装了cent os7作为平常的开发服务器。在平时我工作中主要使用的关系型数据库是MySql,但是作为一个程序员要对技术产生好奇和爱好,PostgreQL也是十分优秀的关系数据库,在MySql被Oracle收购后,PostgresQL的开源属性使它有可能成为新关系型数据库宠儿。
废话不多讲了我们直接进入安装环节,首先我们进入官网的安装教程页面(PostgresQL 官方Cent os7 x86安装教程),按人家的步骤安装就可以了。
下面是实际安装演示:
# 安装PostgresQL yum源
[root@thinkcent thinktik]# yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
Loaded plugins: fastestmirror
pgdg-centos10-10-2.noarch.rpm | 4.6 kB 00:00:00
Examining /var/tmp/yum-root-xbOUDb/pgdg-centos10-10-2.noarch.rpm: pgdg-centos10-10-2.noarch
Marking /var/tmp/yum-root-xbOUDb/pgdg-centos10-10-2.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package pgdg-centos10.noarch 0:10-2 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================
Package Arch Version Repository Size
=========================================================================================================
Installing:
pgdg-centos10 noarch 10-2 /pgdg-centos10-10-2.noarch 2.7 k
Transaction Summary
=========================================================================================================
Install 1 Package
Total size: 2.7 k
Installed size: 2.7 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : pgdg-centos10-10-2.noarch 1/1
Verifying : pgdg-centos10-10-2.noarch 1/1
Installed:
pgdg-centos10.noarch 0:10-2
Complete!
# 安装postgresql
[root@thinkcent thinktik]# yum install postgresql10
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.cn99.com
pgdg10 | 4.1 kB 00:00:00
(1/2): pgdg10/7/x86_64/group_gz | 245 B 00:00:03
(2/2): pgdg10/7/x86_64/primary_db | 162 kB 00:00:03
Resolving Dependencies
--> Running transaction check
---> Package postgresql10.x86_64 0:10.4-1PGDG.rhel7 will be installed
--> Processing Dependency: postgresql10-libs(x86-64) = 10.4-1PGDG.rhel7 for package: postgresql10-10.4-1PGDG.rhel7.x86_64
--> Processing Dependency: libicu for package: postgresql10-10.4-1PGDG.rhel7.x86_64
--> Processing Dependency: libpq.so.5()(64bit) for package: postgresql10-10.4-1PGDG.rhel7.x86_64
--> Running transaction check
---> Package libicu.x86_64 0:50.1.2-15.el7 will be installed
---> Package postgresql10-libs.x86_64 0:10.4-1PGDG.rhel7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================
Package Arch Version Repository Size
=========================================================================================================
Installing:
postgresql10 x86_64 10.4-1PGDG.rhel7 pgdg10 1.5 M
Installing for dependencies:
libicu x86_64 50.1.2-15.el7 base 6.9 M
postgresql10-libs x86_64 10.4-1PGDG.rhel7 pgdg10 354 k
Transaction Summary
=========================================================================================================
Install 1 Package (+2 Dependent packages)
Total download size: 8.8 M
Installed size: 33 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): libicu-50.1.2-15.el7.x86_64.rpm | 6.9 MB 00:00:06
(2/3): postgresql10-libs-10.4-1PGDG.rhel7.x86_64.rpm | 354 kB 00:00:14
(3/3): postgresql10-10.4-1PGDG.rhel7.x86_64.rpm | 1.5 MB 00:01:18
---------------------------------------------------------------------------------------------------------
Total 115 kB/s | 8.8 MB 00:01:18
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : postgresql10-libs-10.4-1PGDG.rhel7.x86_64 1/3
Installing : libicu-50.1.2-15.el7.x86_64 2/3
Installing : postgresql10-10.4-1PGDG.rhel7.x86_64 3/3
Verifying : libicu-50.1.2-15.el7.x86_64 1/3
Verifying : postgresql10-10.4-1PGDG.rhel7.x86_64 2/3
Verifying : postgresql10-libs-10.4-1PGDG.rhel7.x86_64 3/3
Installed:
postgresql10.x86_64 0:10.4-1PGDG.rhel7
Dependency Installed:
libicu.x86_64 0:50.1.2-15.el7 postgresql10-libs.x86_64 0:10.4-1PGDG.rhel7
Complete!
# 安装postgresql10-server(可选)
[root@thinkcent thinktik]# yum install postgresql10-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.cn99.com
Resolving Dependencies
--> Running transaction check
---> Package postgresql10-server.x86_64 0:10.4-1PGDG.rhel7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================
Package Arch Version Repository Size
=========================================================================================================
Installing:
postgresql10-server x86_64 10.4-1PGDG.rhel7 pgdg10 4.5 M
Transaction Summary
=========================================================================================================
Install 1 Package
Total download size: 4.5 M
Installed size: 18 M
Is this ok [y/d/N]: y
Downloading packages:
postgresql10-server-10.4-1PGDG.rhel7.x86_64.rpm | 4.5 MB 00:00:06
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : postgresql10-server-10.4-1PGDG.rhel7.x86_64 1/1
Verifying : postgresql10-server-10.4-1PGDG.rhel7.x86_64 1/1
Installed:
postgresql10-server.x86_64 0:10.4-1PGDG.rhel7
Complete!
# 初始化数据库并设置自启动
[root@thinkcent thinktik]# /usr/pgsql-10/bin/postgresql-10-setup initdb
Initializing database ... OK
[root@thinkcent thinktik]# 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@thinkcent thinktik]# systemctl start postgresql-10
[root@thinkcent thinktik]# ps -ef | grep postgres
postgres 19567 1 0 15:22 ? 00:00:00 /usr/pgsql-10/bin/postmaster -D /var/lib/pgsql/10/data/
postgres 19569 19567 0 15:22 ? 00:00:00 postgres: logger process
postgres 19571 19567 0 15:22 ? 00:00:00 postgres: checkpointer process
postgres 19572 19567 0 15:22 ? 00:00:00 postgres: writer process
postgres 19573 19567 0 15:22 ? 00:00:00 postgres: wal writer process
postgres 19574 19567 0 15:22 ? 00:00:00 postgres: autovacuum launcher process
postgres 19575 19567 0 15:22 ? 00:00:00 postgres: stats collector process
postgres 19576 19567 0 15:22 ? 00:00:00 postgres: bgworker: logical replication launcher
root 19578 19343 0 15:22 pts/0 00:00:00 grep --color=auto postgres
创建用户,创建库
# 切换用户
[root@thinkremote thinktik]# su - postgres
Last login: Wed Jul 18 14:19:01 CST 2018 on pts/2
# 登陆
-bash-4.2$ psql
psql (10.4)
Type "help" for help.
# 查询所有的库
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
# 查询所有库
postgres=# \d
Did not find any relations.
# 创建用户
postgres=# CREATE USER dbuser WITH PASSWORD 'my_password';
CREATE ROLE
# 创建库
postgres=# CREATE DATABASE example_db OWNER dbuser;
CREATE DATABASE
# 赋权
postgres=# GRANT ALL PRIVILEGES ON DATABASE example_db TO dbuser;
GRANT
# 退出
postgres=# \q
-bash-4.2$
设置远程访问
# 修改postgresql.conf,修改端口和监听地址
[root@thinkremote thinktik]# vim /var/lib/pgsql/10/data/postgresql.conf
# - Connection Settings -
# 监听所有ip地址的访问
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
# 监听端口1234
port = 1234 # (change requires restart)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
# 修改postgresql.conf,修改监听地址和验证方式为md5
[root@thinkremote thinktik]# vim /var/lib/pgsql/10/data/pg_hba.conf
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# 加上这一行 允许所有的IP地址访问 密码为md5
host all all 0.0.0.0/0 md5
# 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
防火墙和Selinux放行PostgresQL
# 开放postgresql服务
firewall-cmd --add-service=postgresql --permanent
# 重载防火墙
firewall-cmd –reload
# selinux允许PostgresQL改端口(如果你的linux没有开Selinux就不必执行这句了)
semanage port -a -t postgresql_port_t -p tcp 1234
重启生效
systemctl restart postgresql-10
# 验证端口修改成功
[thinktik@thinkremote ~]$ psql -U dbuser-d example_db -p 1234
psql (10.4)
Type "help" for help.
# 远程连接可以使用datagrip或者navicat等自行验证,我就不讲了