centos8 Docker上安装的postgres

我用的是centos8服务器,在安装postgres数据库先安装docker,这里我安装的是postgres12

centos8 安装 docker

  1. 下载docker-ce的repo

curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
2. 安装依赖(这是相比centos7的关键步骤)

yum install https://download.docker.com/linux/fedora/30/x86_64/stable/Packages/containerd.io-1.2.6-3.3.fc30.x86_64.rpm

  1. 安装docker-ce
    yum install docker-ce
  1. 启动docker
    systemctl start docker

安装之后将postgres从docker hub包管理器pull当前到服务器

docker pull postgres:12

查看镜像 $: docker images
centos8 Docker上安装的postgres

  1. 先创建一个postgresql.conf文件 写在 /data/docker/postgres/下,没有目录就创建

在 postgresql.conf 写入 (注释的不要写)

listen_addresses = '
//监听地址,默认
为当前服务器地址 []docker内部绑定容器需要使用*号 不能使用127 127 是相对于母机的如果跨docker没用 docker内部指向不一样
port = 5432
//postgres使用端口号默认5432
max_connections = 1000
//最大连接数
shared_buffers = 1024MB
wal_level = replica
//置保存操作日志的具体程度级别,postgres10之后有选项三个【minimal,replica,logical】设,要设置WAL归档至少要设置为replica

然后使用docker安装(注释的不要)

$: docker run --name postgres
//设置镜像名字叫做 postgres
-e POSTGRES_PASSWORD=123456
//设置postgres服务器初始密码
-p 5432:5432
//将docker内部端口5432映射绑定到服务器端口5432
-v /data/docker/postgres/data:/var/lib/postgresql/data
//将docker镜像内部文件夹/var/lib/postgresql/data映射绑定到服务器文件夹/data/docker/postgres/data/
-v /data/docker/postgres/buckup:/var/lib/postgresql/buckup
-v /data/docker/postgres/postgresql.conf:/etc/postgresql/postgresql.conf
//将docker镜像内部文件/etc/postgresql/postgresql.conf映射绑定到服务器文件夹/data/docker/postgres/postgresql.conf
-d postgres:12
-c ‘config_file=/etc/postgresql/postgresql.conf’
//指定 postgres使用的配置文件为 /etc/postgresql/postgresql.conf 即上文映射的服务器文件/data/docker/postgres10/postgresql.conf

centos8 Docker上安装的postgres
执行命令。在/data/docker/postgres 下会生成buckup 和data 文件夹

查看 docker ps
centos8 Docker上安装的postgres
说明安装成功。

进入数据库
$: docker exec -it postgres bash (这个postgres 你上面命名的镜像名字)
//切换用户
$: su postgres
//进入数据库指定端口为5432
$: psql -p 5432
查看所有的数据库:\l
会显示数据库就成功了 可以用了
centos8 Docker上安装的postgres

本地连接数据库 我用的是Navicat Premium 12(官网上直接下载,傻瓜式安装就好)

1.第一种方式 关闭服务器上的防火墙 就可以直接连接
//查看防火墙运行状态
$:firewall-cmd --state
关闭防火墙:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop firewalld.service
重启一个服务:systemctl restart firewalld.service
显示一个服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
centos8 Docker上安装的postgres
连接方式
centos8 Docker上安装的postgres

第二种 用ssh通道方式,防火墙就不要开启
这里的密码是你的数据库密码
centos8 Docker上安装的postgres
centos8 Docker上安装的postgres
这里的密码是服务器密码

删除docker

1.查询安装过的包
$:yum list installed | grep docker
centos8 Docker上安装的postgres

2.删除安装的软件包
$:yum -y remove docker-ce.x86_64
$:yum -y remove docker-ce-cli.x86_64

3.删除镜像/容器等
$:rm -rf /var/lib/docker/

4.输入docker或docker --version验证是否卸载
$:docker --version
-bash: /usr/bin/docker: No such file or directory