《Docker系列》Docker部署安装PostGres

13 篇文章 0 订阅
13 篇文章 0 订阅

Docker部署安装PostGres

一、Docker下载镜像

## 使用docker拉取postgres:9.4镜像
[root@zxy_master docker]# docker pull postgres:9.4
9.4: Pulling from library/postgres
......
Digest: sha256:42a7a6a647a602efa9592edd1f56359800d079b93fa52c5d92244c58ac4a2ab9
Status: Downloaded newer image for postgres:9.4
docker.io/library/postgres:9.4

二、Docker查看镜像

## docker命令查看镜像
[root@zxy_master docker]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
postgres     9.4       ed5a45034282   2 years ago    251MB

三、创建容器

执行docker命令创建容器

–name [容器名]

-e POSTGRES_PASSWORD=[postgres密码]

-p ip:ip [使用外部端口54321映射内部端口5432]

-d 选择镜像

[root@zxy_master docker]# docker run --name postgres -e POSTGRES_PASSWORD=postgres -p 54321:5432 -d postgres:9.4
dd50f020b9f434562be9b11cbff3ac866ca8d50475b4caa4c5127e435d22fdd8
## 查看运行中镜像
[root@zxy_master docker]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                         NAMES
dd50f020b9f4   postgres:9.4   "docker-entrypoint.s…"   6 seconds ago   Up 4 seconds   0.0.0.0:54321->5432/tcp, :::54321->5432/tcp   postgres

四、进入容器

[root@zxy_master docker]# docker exec -it dd50f020b9f4 /bin/bash
root@dd50f020b9f4:/# ls

五、操作Postgres

5.1 登录数据库

## 登录数据库
## 默认用户名为postgres,密码为创建容器时自定义
root@dd50f020b9f4:/# psql -U postgres
psql (9.4.26)
Type "help" for help.

5.2 查看所有数据库

## 查看所有数据库
postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
(3 rows)

5.3 模糊查询所有数据库

## 模糊查询所有数据库
postgres=# \l 'post*'
                              List of databases
   Name   |  Owner   | Encoding |  Collate   |   Ctype    | Access privileges
----------+----------+----------+------------+------------+-------------------
 postgres | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
(1 row)

5.4 切换数据库

## 切换数据库
postgres=# \c postgres
You are now connected to database "postgres" as user "postgres".

5.5 查看数据库下schemas

## 查看数据库下schemas
postgres=# \dn
  List of schemas
  Name  |  Owner
--------+----------
 ads    | postgres
 dwd    | postgres
 dws    | postgres
 ods    | postgres
 public | postgres
(5 rows)

5.6 模糊查询schemas

## 模糊查询数据库下schemas
postgres=# \dn '*ds*'
 List of schemas
 Name |  Owner
------+----------
 ads  | postgres
 ods  | postgres
(2 rows)

5.7 显示当前使用schema

## 显示当前使用schema
postgres=# show search_path;
  search_path
----------------
 "$user",public
(1 row)

5.8 切换schema

## 切换当前schema到ods
postgres=# set search_path to ods;
SET
postgres=# show search_path;
 search_path
-------------
 ods
(1 row)

5.9 创建表

## 创建表
postgres=# create table ods.ods_a( id int , name text);
CREATE TABLE

5.10 查询表

## 查询表
postgres=# select * from ods.ods_a;
 id | name
----+------
(0 rows)

5.11 查看表详情

## 查看表详情
postgres=# \d ods.ods_a;
      Table "ods.ods_a"
 Column |  Type   | Modifiers
--------+---------+-----------
 id     | integer |
 name   | text    |

5.12 退出postgres

## 退出psql
postgres=# \q
root@dd50f020b9f4:/# 

六、navicat连接

云服务器需开启54321端口,虚拟机关闭防火墙

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DATA数据猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值