Self-hosting Postgres lets you run the database on your own infrastructure, giving complete control over cost, security, and performance.
Self-hosting removes SaaS fees, allows custom configuration, and fits tighter security policies. Teams can tune memory, CPU, and storage precisely to workload needs.
For small ecommerce setups, 2 vCPU, 4 GB RAM, and SSD storage handle <1 M rows. Scale vertically first, then add read replicas if analytical traffic grows.
Use the official image. One command pulls, configures, and starts Postgres in seconds.
docker run -d --name pg14 \
-e POSTGRES_USER=ecom_admin \
-e POSTGRES_PASSWORD=secret123 \
-e POSTGRES_DB=ecommerce \
-v $PWD/pgdata:/var/lib/postgresql/data \
-p 5432:5432 postgres:14
Connect with psql or any SQL editor and create tables.
psql postgresql://ecom_admin:secret123@localhost:5432/ecommerce -f schema.sql
CREATE TABLE Customers (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE Orders (
id SERIAL PRIMARY KEY,
customer_id INT REFERENCES Customers(id),
order_date DATE NOT NULL,
total_amount NUMERIC(10,2)
);
CREATE TABLE Products (
id SERIAL PRIMARY KEY,
name TEXT,
price NUMERIC(10,2),
stock INT
);
CREATE TABLE OrderItems (
id SERIAL PRIMARY KEY,
order_id INT REFERENCES Orders(id),
product_id INT REFERENCES Products(id),
quantity INT
);
Bind to localhost or a private subnet, use SSL, rotate passwords, and add fail2ban. For Kubernetes, expose via ClusterIP and use Secrets for credentials.
Enable WAL archiving and run nightly pg_dump. Store dumps in object storage (e.g., S3). Test restores monthly with pg_restore
.
Use pg_stat_statements
, Prometheus exporters, and alerts on replication lag, disk usage, and query duration.
Use pg_upgrade
for major releases or spin up a new container, replicate data with logical replication, then switch connections.
Yes. Use different containers or different data directories and ports for native installs (e.g., 5433, 5434).
Start with shared_buffers at 25% RAM and work_mem at 4MB. Benchmark and adjust based on query load.
CPU overhead is negligible; I/O may drop 5-10%. Mount local SSD volumes and use the delegate
driver on macOS for best speed.