CentOS 到了7.x版本, PostgreSQL也来到了10.x版本. 前些天MySQL都直接跨到了8.0版本.
本文是一篇在CentOS 7.4上安装安装PostgreSQL 10.3 的教程. 本文发布于2018-04-28.
1. 切换到root用户:
# su -
2. 把最新的rpm包添加到系统库:
PostgreSQL会为所有的Linux平台发布rpm包, 而且会比其他的的库更新的更快.
可以试试在浏览器输入 https://download.postgresql.org/pub 看会发现什么.
### CentOS 7 ###
# rpm -Uvh https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
### RHEL 7 ###
# rpm -Uvh https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-redhat10-10-2.noarch.rpm
3. 用 yum 安装PostgreSQL:
# yum install -y postgresql10-server postgresql10
4. 安装完成后, 初次使用前先进行初始化:
# /usr/pgsql-10/bin/postgresql-10-setup initdb
PostgreSQL文件默认放在路径: /var/lib/pgsql/10/data/
5. 启动PostgreSQL:
# systemctl start postgresql-10
6. 设置PostgreSQL自启动:
# systemctl enable postgresql-10
7. 查询PostgreSQL运行状态:
# systemctl status postgresql-10
会输出类似的信息:
● postgresql-10.service - PostgreSQL 10 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-10.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2018-04-27 18:03:08 CST; 21h ago
Docs: https://www.postgresql.org/docs/10/static/
Main PID: 14916 (postmaster)
CGroup: /system.slice/postgresql-10.service
├─14916 /usr/pgsql-10/bin/postmaster -D /var/lib/pgsql/10/data/
├─14920 postgres: logger process
├─14922 postgres: checkpointer process
├─14923 postgres: writer process
├─14924 postgres: wal writer process
├─14925 postgres: autovacuum launcher process
├─14926 postgres: stats collector process
└─14927 postgres: bgworker: logical replication launcher
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ systemd[1]: Starting PostgreSQL 10 database server...
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ postmaster[14916]: 2018-04-27 18:03:08.762 CST [14916] LOG: listening on IPv4 address "127.0.0.1...t 5432
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ postmaster[14916]: 2018-04-27 18:03:08.762 CST [14916] LOG: could not bind IPv6 address "::1": C...ddress
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ postmaster[14916]: 2018-04-27 18:03:08.762 CST [14916] HINT: Is another postmaster already runni...retry.
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ postmaster[14916]: 2018-04-27 18:03:08.764 CST [14916] LOG: listening on Unix socket "/var/run/p....5432"
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ postmaster[14916]: 2018-04-27 18:03:08.767 CST [14916] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ postmaster[14916]: 2018-04-27 18:03:08.777 CST [14916] LOG: redirecting log output to logging co...rocess
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ postmaster[14916]: 2018-04-27 18:03:08.777 CST [14916] HINT: Future log output will appear in di..."log".
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ systemd[1]: Started PostgreSQL 10 database server.
Hint: Some lines were ellipsized, use -l to show in full.
8. 再确认看看PostgreSQL是否运行在5432端口:
# netstat -antup | grep 5432
会有类似输出:
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 14916/postmaster
9. 连接PostgreSQL服务器:
PostgreSQL会自动创建postgres用户, 创建数据库之前, 要用postgres用户或root用户登录并重置postgres用户密码.
# su -l postgres
10. 连接数据库, psql
命令会激活PostgreSQL数据库终端:
$ psql
会输出下列说明连接进入了PostgreSQL数据库:
psql (10.3)
Type "help" for help.
postgres=#
11. 重置postgres用户密码:
postgres=# \password
文章来源并整理自IT's Ggeek By Raj.