DEV Community

Welcoming Sloth
Welcoming Sloth

Posted on Edited on

Quickly create a local postgres database with docker

Here are some quick package.json scripts to create, remove, start, and stop a local docker postgres database.

{
  "scripts": {
    "db:create": "docker run --name DB_NAME -e POSTGRES_PASSWORD=postgres -p DB_EXTERNAL_PORT:5432 -d postgres",
    "db:remove": "docker rm DB_NAME",
    "db:start": "docker start DB_NAME",
    "db:stop": "docker stop DB_NAME"
  }
}
Enter fullscreen mode Exit fullscreen mode

Used env vars:
DB_NAME: Name for the docker container to create.
DB_EXTERNAL_PORT: What local port the database should be accessible from. Use 5432 for default pg port.

Top comments (2)

Collapse
 
paulsmithkc profile image
Paul Smith •
  1. Might want to document the env variables to use this.
  2. Install instruction for docker + postgres would help to flesh this out.
  3. Link to your sources, both to give credit, and to give readers more resources.
Collapse
 
caravaggio profile image
Welcoming Sloth •
  1. Good point.
  2. This is more of a cheat-sheet. This assumes you already have docker installed & know how it works. If you don't there are plenty of guides available just a search away for how to install it on your specific system. This doesn't require any psql tools to be installed, but again, it's just a search away.
  3. Could you elaborate on resources? Again, this is more a cheat-sheet / quick-reference and assumes you already know what docker and pg is.