Use External Drive Postgresql Docker Wsl2

20 Sep 2023 - Alejandro Piña

Using a docker postgresql image with external drive in WSL2.


  • Docker PostreSQL container
  • WSL2 installed
  • External drive connected

Make sure that your external drive has a format ext4 and mounted in WSL2 if you external drive has other format follow the next steps to create a new partition to be used as ext4

Create a partition file

The partition file is going to make with a block size of 4 KiB multiplied by count parameters in the following example is the equivalent to 1 Terabyte

dd if=/dev/zero of=/mnt/d/ext4_01.img bs=4K count=268435456 status=progress

Format the file

mkfs.ext4 /mnt/d/ext4_01.img

Create a mount point in WSL

sudo mkdir /mnt/ext4_ext

Mount the new partition

sudo mount -o loop /mnt/d/ext4_01.img /mnt/ext4_ext

Give permission to your user

sudo chown $USER:$USER /mnt/ext4_ext

Create a folder to save your databases

mkdir /mnt/ext4_ext/postgres_data

Create and run your postgres image using your new partition

docker run -d \
    --name db-postgres-data \
    -e POSTGRES_PASSWORD=mypassword \
    -v /mnt/ext4_ext/postgres_data:/var/lib/postgresql/data \
    -p 5432:5432 \