[DevOps] Set up and run a PostgreSQL instance locally with Docker Compose

When we need to spin up a database instance for our new project, installing the database management system directly on our local machine is almost always a bad idea. Luckily, Docker is here to help us practically reduce the complexity of dealing with "missing dependencies" and weird error messages down to zero.

In this lesson, we will learn how to get a PostgreSQL instance running locally with Docker Compose, so we can work on the database integration of our project and test things out with ease. To follow this lesson, you will need to have Docker pre-installed.

 

docker-compose.yml:

复制代码
version: "3.8"
services:
  db:
    image: "postgres:12"
    ports:
      - "54320:5432"
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=alice
      - POSTGRES_PASSWORD=wonderland
      - POSTGRES_DB=myawesomedb
复制代码

 

 

复制代码
# Up and Running

docker-compose -d

# Check Whether contain is running

docker-compose ps

# enter the shell

docker-compose run db bash

## Verify db exist

poql --host=db --username=alice --dbname=myawesomedb

## Esc the db

Ctrl + d twice

# Off

docker-compose down
复制代码

 

posted @   Zhentiw  阅读(141)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-05-18 [Algorithm] Area of polygon
2017-05-18 [SCSS] Write Custom Functions with the SCSS @function Directive
2017-05-18 [SCSS] Loop Over Data with the SCSS @each Control Directive
2016-05-18 [PWA] 14. Loop cursor
2016-05-18 [PWA] 13. New db and object store
2016-05-18 [PWA] 12. Intro to IndexedDB
2016-05-18 [PWA] 11. Serve skeleton cache for root
点击右上角即可分享
微信分享提示