---
# System prepended metadata

title: Install PostgreSQL in Docker
tags: [Docker]

---

# Install PostgreSQL in Docker

## Steps:

### 1. Create Docker Volume
```shell=
docker volume create --name postgres-sql-data
```

### 2. Create Container with admin account
```shell=
docker run \
       -d \
       --name=postgres \
       -p 5432:5432 \
       -e POSTGRES_USER="{admin_username}" \
       -e POSTGRES_PASSWORD="{admin_username}" \
       -v postgres-sql-data:/var/lib/postgresql/data \
       --restart=always \
       postgres
```
**Example:**
```shell=
docker run \
       -d \
       --name=postgres \
       -p 5432:5432 \
       -e POSTGRES_USER="admin" \
       -e POSTGRES_PASSWORD="123456" \
       -v postgres-sql-data:/var/lib/postgresql/data \
       --restart=always \
       postgres
```

### 3. Executed in docker and login into PostgreSQL
```shell=
docker exec -it postgres psql -U {admin_username}
```
**Example:**
```shell=
docker exec -it postgres psql -U admin
```

### 4. Create new user with SUPERUSER role
```sql=
CREATE USER {username} WITH PASSWORD '{userpassword}';
ALTER USER {username} WITH SUPERUSER;
```
**Example:**
```sql=
CREATE USER yuze WITH PASSWORD '123456';
ALTER USER yuze WITH SUPERUSER;
```
###### tags: `Docker`