PostgreSQL—下载安装与使用

一、PostgreSQL 下载安装

1、安装PostgreSQL仓库(添加yum源)

[root@localhost ~]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm                      # 方式一
[root@localhost ~]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %{rhel})-x86_64/pgdg-redhat-repo-latest.noarch.rpm      # 方式二
[root@localhost ~]# yum install -y https://download.postgresql.org/pub/repos/yum/12/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm                   # 方式三

2、 安装PostgreSQL服务端

[root@localhost ~]# yum install -y postgresql12                                               (客户端安装)
[root@localhost ~]# yum install -y postgresql12-server                                        (服务端安装)
[root@localhost ~]# yum install -y postgresql12 postgresql12-server postgresql12-contrib      (一步到位,相当于上面两步)

3、初始化数据库:使用默认数据目录,yum安装的postgresql的默认数据目录为 /var/lib/pgsql/12/data,直接初始化就行。

[root@localhost ~]# /usr/pgsql-12/bin/postgresql-12-setup initdb

4、初始化数据库:使用指定数据目录,创建目录、增加用户、给用户赋权、初始化数据库。与步骤3选择其中一个即可。

[root@localhost ~]# mkdir -p /data/postgresql/data                        # 新建数据目录
[root@localhost ~]# useradd postgres                                      # 创建用户
[root@localhost ~]# chown -R postgres:postgres /data/postgresql/data      # 授权,给创建的目录赋予 postgres 用户权限
[root@localhost ~]# su - postgres                                         # 切换到postgres用户

[root@localhost ~]# /usr/pgsql-12/bin/initdb -D /data/postgresql/data     # 初始化数据库

[root@localhost ~]# vim /usr/lib/systemd/system/postgresql-12.service     # 修改postgresql的system.service配置文件:修改如下
Environment=PGDATA=/data/postgresql/data/
[root@localhost ~]# systemctl daemon-reload                               # 重新加载系统服务

5、启动 PostgreSQL 服务

[root@localhost ~]# systemctl enable postgresql-12
[root@localhost ~]# systemctl start postgresql-12
[root@localhost ~]# systemctl status postgresql-12

postgresql会自动完成以下操作:

  • 自动生成一个linux系统用户postgres:管理数据库的系统用户。
  • 数据库用户postgres:数据库超级管理员,此用户的默认数据库为postgres。

6、修改数据库账户postgres默认密码

[root@localhost ~]# su - postgres      # 或者:sudo -i -u postgres
[postgres@localhost ~]# psql           # 进入postgresql命令行
postgres=# alter user postgres password '123456'    # alter role postgres with password '123456';

7、修改配置文件允许远程连接

[postgres@localhost ~]# vim /data/postgresql/data/postgresql.conf       # 使用指定数据目录
[postgres@localhost ~]# vim /var/lib/pgsql/12/data/postgresql.conf      # 使用默认数据目录
listen_addresses = '*'               # 修改监听的ip
port = 5432                          # 修改监听的端口

[postgres@localhost ~]# vim /data/postgresql/data/pg_hba.conf         # 使用指定数据目录
[postgres@localhost ~]# vim /var/lib/pgsql/12/data/pg_hba.conf        # 使用默认数据目录
host all all 0.0.0.0/0 password      # 所有的用户通过任意IP都可以使用面膜的方式登录PostgreSQL

8、数据库服务器开启、重启和状态查看

[root@localhost ~]# systemctl stop postgresql-12         # 停止服务
[root@localhost ~]# systemctl start postgresql-12        # 启动服务
[root@localhost ~]# systemctl status postgresql-12       # 服务状态
[root@localhost ~]# systemctl restart postgresql-12      # 重启服务

[root@localhost ~]# netstat -lnput                       # 服务状态

二、客户端连接工具下载:

Navicat:http://www.navicat.com.cn/download/navicat-for-postgresql

pgAdmin:https://www.pgadmin.org/download/

二、PostgreSQL 配置文件

三、PostgreSQL 常用命令

1、安装完毕后,系统会创建一个数据库超级用户 postgres,密码为空。首先切换到 postgres 用户。

[root@localhost ~]# sudo -i -u postgres

2、使用 psql 命令进入 postgres 命令行。

[postgres@localhost ~]# psql

3、输入 exit 命令退出 postgres 命令行。

postgres=# \q        # 退出方式一
postgres=# exit      # 退出方式二

4、远程连接访问 postgresql 数据库

[root@localhost ~]# psql -h IPadrress -p 1921 -U username -d database
[root@localhost ~]# psql -h localhost -p 5432 -U postgress runoobdb                         # 5432:默认端口,可以指定端口如:1921。
[root@localhost ~]# psql -h localhost -p 5432 -U postgress -d runoobdb                      # runoobdb:数据库名称(可以省略-d选项)。
[root@localhost ~]# psql -h pgm-uf6mix.pg.rds.aliyuncs.com -p 1921 -U postgres -d shop      # 1921:指定端口,postgres:用户名,shop:数据库名称。

5、

https://www.icode9.com/content-2-729145.html

https://blog.csdn.net/wc1695040842/article/details/106780666

https://shixiongfei.com/centos-install-postgresql-timescaledb.html

https://blog.csdn.net/weixin_42290927/article/details/112476306

https://blog.csdn.net/weixin_42514606/article/details/87565307

posted @ 2021-01-16 14:31  刘_love_田  阅读(2034)  评论(0编辑  收藏  举报