Gist
Running PostgreSQL Locally with Docker
Run PostgreSQL locally with Docker: start a Postgres container, get connection strings (psql, Prisma, Node pg, SQLAlchemy), view logs, create databases/users, and manage containers for local development.
Running PostgreSQL Locally with Docker
A quick reference for starting a local PostgreSQL server using Docker, creating databases, and connecting from applications.
🚀 Start a Local PostgreSQL Server
Basic docker run command
docker run --name local-postgres \\
-e POSTGRES_PASSWORD=postgres \\
-e POSTGRES_USER=postgres \\
-e POSTGRES_DB=postgres \\
-p 5432:5432 \\
-d postgres:16
🔗 Connection Strings
Standard PostgreSQL URI
postgresql://postgres:postgres@localhost:5432/postgres
SQLAlchemy
postgresql+psycopg2://postgres:postgres@localhost:5432/postgres
Node (pg library)
const connectionString = "postgres://postgres:postgres@localhost:5432/postgres";
Prisma
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/postgres?schema=public"
Useful Docker Commands
View logs
docker logs -f local-postgres
Stop and remove container
docker stop local-postgres
docker rm local-postgres
Creating and Accessing Additional Databases
Enter psql inside the container
docker exec -it local-postgres psql -U postgres
Create a new database
CREATE DATABASE myappdb;
List databases
\l
Create DB Without Entering psql
docker exec -it local-postgres createdb -U postgres myappdb
Connection String for the New Database
postgresql://postgres:postgres@localhost:5432/myappdb
Create a New User and Grant Access
CREATE USER myuser WITH PASSWORD 'mypassword';
GRANT ALL PRIVILEGES ON DATABASE myappdb TO myuser;
Connection string:
postgresql://myuser:mypassword@localhost:5432/myappdb