Docker部署安装PostGres
Posted Mr.zhou_Zxy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker部署安装PostGres相关的知识,希望对你有一定的参考价值。
Docker部署安装PostGres
一、下载
## 使用docker拉取postgres:9.4镜像
[root@zxy_master docker]# docker pull postgres:9.4
9.4: Pulling from library/postgres
619014d83c02: Pull complete
7ec0fe6664f6: Pull complete
9ca7ba8f7764: Pull complete
9e1155d037e2: Pull complete
febcfb7f8870: Pull complete
8c78c79412b5: Pull complete
5a35744405c5: Pull complete
27717922e067: Pull complete
36f0c5255550: Pull complete
dbf0a396f422: Pull complete
ec4c06ea33e5: Pull complete
e8dd33eba6d1: Pull complete
51c81b3b2c20: Pull complete
2a03dd76f5d7: Pull complete
Digest: sha256:42a7a6a647a602efa9592edd1f56359800d079b93fa52c5d92244c58ac4a2ab9
Status: Downloaded newer image for postgres:9.4
docker.io/library/postgres:9.4
二、查看images
## 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,密码为创建容器时自定义
root@dd50f020b9f4:/# psql -U postgres
psql (9.4.26)
Type "help" for help.
## 1.查看所有数据库
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)
## 2.模糊查询所有数据库
postgres=# \\l 'post*'
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------+----------+----------+------------+------------+-------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
(1 row)
## 3.切换数据库
postgres=# \\c postgres
You are now connected to database "postgres" as user "postgres".
## 4.查看数据库下schemas
postgres=# \\dn
List of schemas
Name | Owner
--------+----------
ads | postgres
dwd | postgres
dws | postgres
ods | postgres
public | postgres
(5 rows)
## 5.模糊查询数据库下schemas
postgres=# \\dn '*ds*'
List of schemas
Name | Owner
------+----------
ads | postgres
ods | postgres
(2 rows)
## 6.显示当前使用schema
postgres=# show search_path;
search_path
----------------
"$user",public
(1 row)
## 7.切换当前schema到ods
postgres=# set search_path to ods;
SET
postgres=# show search_path;
search_path
-------------
ods
(1 row)
## 8.创建表
postgres=# create table ods.ods_a( id int , name text);
CREATE TABLE
## 9.查询表
postgres=# select * from ods.ods_a;
id | name
----+------
(0 rows)
## 10.查看表详情
postgres=# \\d ods.ods_a;
Table "ods.ods_a"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
name | text |
## 11.退出psql
postgres=# \\q
root@dd50f020b9f4:/#
五、navicat连接
云服务器需开启54321端口,虚拟机关闭防火墙
以上是关于Docker部署安装PostGres的主要内容,如果未能解决你的问题,请参考以下文章
用docker部署postgres 10 + postgis 2.4.1