【postgres】CentOS7.x上docker安裝postgres

一、實(shí)驗(yàn)環(huán)境

操作系統(tǒng): CentOS7.5 Mininmal

IP: 192.168.1.104

docker版本:18.06.0-ce

posgres鏡像版本:9.6


二、安裝docker


關(guān)閉selinux

# setenforce 0

# sed? -i? 's/^SELINUX=.*/SELINUX=permissive/g'? /etc/selinux/config


下載docker二進(jìn)制安裝包

# yum? -y install? ?wget?

# wget? ?https://download.docker.com/linux/static/stable/x86_64/docker-18.06.0-ce.tgz

#? tar -zxf? ?docker-18.06.0-ce.tgz

#? ll? ?./docker

# cp ./docker/docker*? ?/usr/bin

?創(chuàng)建docker服務(wù)的unit文件

# vim ?/etc/systemd/system/docker.service

##############################################################

[Unit]

Description=Docker Application Container Engine

Documentation=https://docs.docker.com

After=network-online.target firewalld.service

Wants=network-online.target

[Service]

Type=notify

# the default is not to use systemd for cgroups because the delegate issues still

# exists and systemd currently does not support the cgroup feature set required

# for containers run by docker

ExecStart=/usr/bin/dockerd

ExecReload=/bin/kill -s HUP $MAINPID

# Having non-zero Limit*s causes performance problems due to accounting overhead

# in the kernel. We recommend using cgroups to do container-local accounting.

LimitNOFILE=infinity

LimitNPROC=infinity

LimitCORE=infinity

# Uncomment TasksMax if your systemd version supports it.

# Only systemd 226 and above support this version.

#TasksMax=infinity

TimeoutStartSec=0

# set delegate yes so that systemd does not reset the cgroups of docker containers

Delegate=yes

# kill only the docker process, not all processes in the cgroup

KillMode=process

# restart the docker process if it exits prematurely

Restart=on-failure

StartLimitBurst=3

StartLimitInterval=60s

[Install]

WantedBy=multi-user.target

##############################################

啟動(dòng)docker服務(wù),設(shè)置開機(jī)自啟

# systemctl daemon-reload

# systemctl start docker

# systemctl ?status docker

# systemctl enable docker

# docker ?info

# docker ?version

設(shè)置鏡像加速

#? curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

#? systemctl restart docker


三、初始化Postgres


拉取postgre 9.6 版本鏡像

# docker pull postgres:9.6

# docker images

# docker run -it --rm?postgres:9.6? cat /etc/issue

# docker run -it --rm postgres:9.6 cat /etc/passwd

創(chuàng)建postgres數(shù)據(jù)存儲(chǔ)目錄

#? mkdir? ?-p? ?/var/lib/postgresql/data


創(chuàng)建postgres容器

#?docker run -it \

? --name postgres \

-e??POSTGRES_USER="postgres" \

-e?POSTGRES_DB="postgres" \

? -e POSTGRES_PASSWORD="Postgres@123"? \

? -v /var/lib/postgresql/data:/var/lib/postgresql/data \

? -p 5432:5432 \

? postgres:9.6


此處容器創(chuàng)建我們用到了幾個(gè)環(huán)境變量:

-e??POSTGRES_USER="postgres"?

-e?POSTGRES_DB="postgres"?

?-e POSTGRES_PASSWORD="Postgres@123"

表示創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)管理員用戶 postgres,初始庫(kù)為postgres,密碼設(shè)置為Postgres@123

更多環(huán)境變量參考官方文檔:https://docs.docker.com/samples/library/postgres

# docker ps -a

# ss? -tan? | grep? 5432

開放相關(guān)端口

#? firewall-cmd --zone=public? --add-port=5432/tcp --permanent

#? firewall-cmd --reload


命令行登錄數(shù)據(jù)庫(kù)

# docker exec -it -u postgres? ?postgres? ?bash

$ id

$ psql

postgres=# \l

創(chuàng)建的數(shù)據(jù)庫(kù),默認(rèn)開放的用戶的遠(yuǎn)程登錄



查看初始化的生成的數(shù)據(jù)庫(kù)目錄下數(shù)據(jù)

# ll /var/lib/postgresql/

# ll /var/lib/postgresql/data/

可以看出,我們創(chuàng)建的postgres的生成的持久化存儲(chǔ)的文件中包括了postgres的配置文件:

/var/lib/postgresql/data/postgresql.conf

/var/lib/postgresql/data/pg_hba.conf

/var/lib/postgresql/data/pg_ident.conf

如果需做相關(guān)配置變更,更改相應(yīng)的文件,重啟服務(wù)生效。


四、將postgres服務(wù)注冊(cè)成系統(tǒng)服務(wù)


刪掉初始化容器

# docker stop postgres

# docker rm postgres


創(chuàng)建postgres的systemd Unit文件

# vim /etc/systemd/system/postgres.service

#######################################################

[Unit]

Description=Postgres Server

After=network-online.target docker.service

Requires=docker.service

[Service]

ExecStartPre=-/usr/bin/docker rm -f postgres

ExecStart=/usr/bin/docker run \

? --name postgres \

? -p 5432:5432 \

? -v /var/lib/postgresql/data:/var/lib/postgresql/data \

? postgres:9.6

ExecStop=/usr/bin/docker stop postgres

LimitNOFILE=65535

Restart=on-failure

StartLimitBurst=3

StartLimitInterval=60s

[Install]

WantedBy=multi-user.target

########################################################

你應(yīng)該注意到了,Unit文件中,docker run的參數(shù)中并不需要初始化那會(huì)的環(huán)境變量了

因?yàn)槌跏蓟瘯r(shí)已持久化存儲(chǔ)在數(shù)據(jù)庫(kù)目錄中了,啟動(dòng)時(shí)會(huì)去讀取,不用再以明文形式寫入文件,增強(qiáng)了安全性!


重新啟動(dòng)服務(wù)

#?systemctl daemon-reload

#?systemctl start postgres

#?systemctl status postgres

#?systemctl enable postgres


五、網(wǎng)頁(yè)版postgreSQL數(shù)據(jù)庫(kù)管理工具

pgAdmin?是一個(gè)非常流行、功能強(qiáng)大并且開源的 PostgreSQL 管理與開發(fā)平臺(tái)。

pgAdmin 支持 Linux、Unix、Mac OS X 以及 Windows 操作系統(tǒng),可以管理 PostgreSQL 9.2 以及更高版本。


pgAdmin 4 (Windows)

https://www.pgadmin.org/download/pgadmin-4-windows

http://127.0.0.1:59484/browser/#


pgAdmin 支持多語(yǔ)言界面,默認(rèn)為英語(yǔ)。

我們將其改為中文界面,點(diǎn)擊“File”下拉菜單,選擇“Preferences”,找到左側(cè)列表中的“miscellaneous” -> “User language”,然后在右側(cè)下拉列表中選項(xiàng)“Chinese (Simplified)”。


最后點(diǎn)擊“Save”保存設(shè)置,需要重新打開或者刷新瀏覽器才能生效



六、參考

postgres

https://docs.docker.com/samples/library/postgres


【Docker】CentOS7.x上docker的安裝方式

http://www.itdecent.cn/p/d1a07a13a76f


【Docker&MySQL】CentOS7.x上容器部署MySQL

http://www.itdecent.cn/p/c74b1d2d8ad4


Dockerfile to build a PostgreSQL container image which can be linked to other containers

https://github.com/sameersbn/docker-postgresql


命令行方式登錄PostgreSQL

https://www.cnblogs.com/xxfcz/p/6483892.html


PostgreSQL 管理工具之 pgAdmin

https://blog.csdn.net/horses/article/details/90665711

PostgreSQL pg_hba.conf 文件簡(jiǎn)析

https://www.cnblogs.com/hiloves/archive/2011/08/20/2147043.html


PostgreSQL pg_ident.conf 文件簡(jiǎn)析

https://www.cnblogs.com/hiloves/archive/2011/08/24/2152144.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容