博客地址
Github
1. 查看yum数据源
https://yum.postgresql.org/repopackages.php
2. 安装10.6数据库源
$ yum install -y https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
3. 安装Postgres
# 先查看postgresql源
$ yum list|grep postgresql
# 安装
$ yum install postgresql10-contrib postgresql10-server -y
4. 初始化
$ /usr/pgsql-10/bin/postgresql-10-setup initdb
5. 启动
$ systemctl start postgresql-10
$ systemctl enable postgresql-10.service
6. 登录postgresql并设置密码
# 切换到postgres用户
$ su postgres
# 进入
$ psql -U postgres
# 设置密码
$ ALTER USER postgres WITH PASSWORD '密码';
退出:\q
备注其他:列出所有库\l 列出所有用户\du 列出库下所有表\d
7. 修改配置,支持远程登陆
$ vi /var/lib/pgsql/10/data/pg_hba.conf
### 再TYPE DATABASE下增加一条
host all all 0.0.0.0/0 md5
$ vi /var/lib/pgsql/10/data/postgresql.conf
# 修改listen_address = ''
listen_address = '*'
# 重启生效
$ systemctl restart postgresql-10
8. 其他基础命令
\h:查看SQL命令的解释,比如\h select。
\?:查看psql命令列表。
\l:列出所有数据库。
\c [database_name]:连接其他数据库。
\d:列出当前数据库的所有表格。
\d [table_name]:列出某一张表格的结构。
\du:列出所有用户。
\e:打开文本编辑器。
\conninfo:列出当前数据库和连接的信息。
参考资料:
https://www.jianshu.com/p/8515a02492f1