打开 PostgreSQL 官网 https://www.postgresql.org/,点击菜单栏上的 Download ,可以看到这里包含了很多平台的安装包,包括 Linux、Windows、Mac OS等 。
Linux 我们可以看到支持 Ubuntu 和 Red Hat 等各个平台,点击具体的平台链接,即可查看安装方法:
经过上述的选择,得到的安装步骤如下:
# Install the repository RPM:
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# Install PostgreSQL:
yum install postgresql12-server
# Optionally initialize the database and enable automatic start:
/usr/pgsql-12/bin/postgresql-12-setup initdb
systemctl enable postgresql-12
systemctl start postgresql-12
启动后默认监听的地址是:127.0.0.1,端口是5432
[root@docker ~]# systemctl start postgresql-12
[root@docker ~]# systemctl status postgresql-12
● postgresql-12.service - PostgreSQL 12 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2020-07-27 11:42:02 CST; 6s ago
Docs: https://www.postgresql.org/docs/12/static/
Process: 1628 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 1635 (postmaster)
CGroup: /system.slice/postgresql-12.service
├─1635 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
├─1637 postgres: logger
├─1639 postgres: checkpointer
├─1640 postgres: background writer
├─1641 postgres: walwriter
├─1642 postgres: autovacuum launcher
├─1643 postgres: stats collector
└─1644 postgres: logical replication launcher
Jul 27 11:42:02 docker.domain.com systemd[1]: Starting PostgreSQL 12 database server...
Jul 27 11:42:02 docker.domain.com postmaster[1635]: 2020-07-27 11:42:02.505 CST [1635] LOG: starting PostgreSQL 12.3 on x86_64-pc-linux-g... 64-bit
Jul 27 11:42:02 docker.domain.com postmaster[1635]: 2020-07-27 11:42:02.506 CST [1635] LOG: listening on IPv4 address "127.0.0.1", port 5432
Jul 27 11:42:02 docker.domain.com postmaster[1635]: 2020-07-27 11:42:02.506 CST [1635] LOG: could not bind IPv6 address "::1": Cannot ass...address
Jul 27 11:42:02 docker.domain.com postmaster[1635]: 2020-07-27 11:42:02.506 CST [1635] HINT: Is another postmaster already running on por... retry.
Jul 27 11:42:02 docker.domain.com postmaster[1635]: 2020-07-27 11:42:02.507 CST [1635] LOG: listening on Unix socket "/var/run/postgresql...L.5432"
Jul 27 11:42:02 docker.domain.com postmaster[1635]: 2020-07-27 11:42:02.508 CST [1635] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
Jul 27 11:42:02 docker.domain.com postmaster[1635]: 2020-07-27 11:42:02.516 CST [1635] LOG: redirecting log output to logging collector process
Jul 27 11:42:02 docker.domain.com postmaster[1635]: 2020-07-27 11:42:02.516 CST [1635] HINT: Future log output will appear in directory "log".
Jul 27 11:42:02 docker.domain.com systemd[1]: Started PostgreSQL 12 database server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@docker ~]# ss -tulnp | grep 5432
tcp LISTEN 0 128 127.0.0.1:5432 *:* users:(("postmaster",pid=1635,fd=3))
安装完毕后,系统会创建一个数据库超级用户 postgres,密码为空。
[root@docker ~]# su - postgres
使用以下命令进入 postgres,输出以下信息,说明安装成功:
[root@docker ~]# su - postgres
-bash-4.2$ psql
psql (12.3)
Type "help" for help.
postgres=#
输入以下命令退出 PostgreSQL 提示符
postgres=# \\q
-bash-4.2$ exit
logout
相关命令
systemctl enable postgresql-12
systemctl start postgresql-12
systemctl stop postgresql-12
systemctl status postgresql-12
systemctl restart postgresql-12
相关文件路径:
主程序路径:/usr/pgsql-12/bin/
配置文件路径:/var/lib/pgsql/12/data/
日志文件路径:/var/lib/pgsql/12/data/log