---
# System prepended metadata

title: 樹莓派筆記-Postgres
tags: [docker, Linux, 樹莓派]

---

---
title: 
description: 
date: 2023-04-24
lang: zh-tw
tags:
  - 樹莓派
  - Linux
  - docker
---
# 樹莓派筆記-Postgres

## docker-compose
`docker-compose.yml`
```yml=
version: "3.7"
networks:
  default:
    name: ${DOCKER_MY_NETWORK}
    external: true
services:
  postgres:
    container_name: postgres
    image: postgres:13.2-alpine
    restart: unless-stopped
    env_file:
      - stack.env
    volumes:
      - ${HOME_PATH}/postgres:/var/lib/postgresql/data
    expose:
      - 5432
  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    restart: unless-stopped
    user: root
    env_file:
      - stack.env
    volumes:
      - ${HOME_PATH}/pgadmin:/var/lib/pgadmin
    expose:
      - 80
    depends_on:
      - postgres
```


## .env 
`stack.env`
```env=
DOCKER_MY_NETWORK=caddy_net
HOME_PATH=<your path>
TZ=Asia/Taipei
PGDATA=/var/lib/postgresql/data
POSTGRES_DB=postgres
POSTGRES_USER=postgres

#用這個創建隨機密鑰 
https://www.grc.com/passwords.htm
POSTGRES_PASSWORD="POSTGRES預設帳號密碼"
PGADMIN_DEFAULT_EMAIL="admin@admin.com"
PGADMIN_DEFAULT_PASSWORD="PGADMIN預設帳號密碼"
```
## Caddyfile
添加以下
`Caddyfile`
```Caddyfile=
pgadmin.{$MY_DOMAIN} {
        reverse_proxy pgadmin:80 
}
```

## pgadmin 使用
pgadmin是Postgres的GUI管理器
先用設定好了帳號登入後

### 連上Postgres
![image alt](https://i.imgur.com/GzNSFKV.png)

### 登入資訊:
Name: postgres
Host name/address: postgres
Port: 5432
Username: postgres
Password: env中上面設定的

![image alt](https://i.imgur.com/RPxbask.png)


### 創建新資料庫

![image alt](https://i.imgur.com/tANAYzr.png)

輸入接下來會用這個資料庫的名子並創建
例如: LLDAP

### 使用資料庫

在對應的選項填上 Postgres url
以剛剛創建的LLDAP為例:

```
postgres://Username:Password@postgres:5432/DateBase-Name
```
> Username=postgres #env設定的
> Password=<密碼> #env設定的
> DateBase-Name=LLDAP #剛剛創建的DB

## Ref
* https://hub.docker.com/_/postgres
* https://hub.docker.com/r/dpage/pgadmin4/