1.在linux執行以下代碼:
docker run -p 5432:5432 -v /home/docker/postgresql/data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=postgres -e TZ=PRC -d --name=some-postgres postgres:11.5-alpine
-p端口映射
-v將數據存到宿主服務器
-e POSTGRES_PASSWORD 密碼(默認用戶名postgres)
-e TZ=PRC時區,中國
-d后台運行
--name容器名稱
2.時區問題
如果在啟動容器時不設置時區,默認為UTC,使用now()設置默認值的時候將有時間差。
select now(); show time zone;
在設置環境變量時,提示:
Warning: the Docker specific variables will only have an effect if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup.
意思是:數據目錄為空時,設置環境變量有效;數據目錄不為空時,將使用數據目錄設置的環境變量,將保持不變。
例如上面代碼的宿主服務器/home/docker/postgresql/data就是數據目錄。
3.參考
dockerhub安裝說明
https://hub.docker.com/_/postgres?tab=description
使用非docker安裝postgresql安裝設置時區,對docker安裝可以參考,例如:postgresql.conf配置在宿主服務器/home/docker/postgresql/data下,設置同樣有效
https://blog.csdn.net/max1124/article/details/78410315
https://github.com/docker-library/postgres/issues/137#issuecomment-217064811