TABLE OF CONTENTS
Bye Bye RDS, Hello Docker + Postgres
January 04, 2020
A little over a year ago, December 2018, is around when I really got serious about learning programming. One of the things I did to help motivate myself to learn was sign up for Amazon Web Service’s free year. In my head, it was basically a race against time to make best use of AWS before I started having to pay.
Fast forward through the free year, I dabbled a tiny bit with CloudFront, did a lot of manual uploading to S3 for my first (now deprecated) website, failed to really learn Lambda and API Gateway, and essentially just discovered RDS a bit too late.
Two weeks ago, I wanted to figure out two things that I didn’t really know:
- How to structure a database, which for me was inspired by a frontend library doc - Redux: Normalizing State Shape
- Animations based on SSR data & events.
I crammed a ton, and built a GraphQL Apollo API, connected to an RDS instance. I also extended my blog’s code with several UI features to interfance with this new API.
…Then I got a bill from AWS.
AIN’T NOBODY GOT TIME TO PAY FOR A NOT-PROFITTING DATABASE. My AWS free year was over. It was time to find a free alternative, because free is good.
I decided to look into this for the sake of continuing local development and learning (this is the most important thing). Of course, my production blog would no longer be talking to any database, but the traffic that my blog gets is…
The whole time while developing the API, I was doing work against an RDS instance. I just needed a few values from my AWS RDS console, and TypeORM would handle the connection for my GraphQL server. But now that RDS was being replaced by a local Docker container, I had to learn some new things.
I found my answer in this stack overflow post, but I wanted to understand what the commands I was running were actually doing. (A while ago, I did a quick run through of Docker commands for myself. But with life as we know it, I forgot everything. Ha.)
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres ├ [Options] ├ IMAGE[:TAG|@DIGEST] # --name (Container Name) postgres # -e (EnvirontmentVariable=Value) -d (Detached mode) # -p (Host:Container) 6db70e055dee9bb1c96a8aaec319c283d3711a2792caaf31995cda05ca162967
Here’s my understanding of what this command is doing, in english.
Now the container is available at
docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6db70e055dee postgres "docker-entrypoint.s…" 5 seconds ago Up 3 seconds 0.0.0.0:5432->5432/tcp some-postgres
Hey Docker, list all my containers
docker exec -it 6db70e055dee bash ├ [Options] # -i (Interactive) # -t (Psuedo Terminal) ├ [Container] # 6db70e055dee ├ [Command] # bash # root@6db70e055dee:/#
Hey Docker, I want you to run
bashin my container
6db70e055dee, and keep the STDIN open, and allocate a psuedo terminal.
At this point I’m in an interactive bash process, inside my Docker container!
Now this is leaving Docker territory, and going into postgresql’s interactive terminal territory.
root@6db70e055dee:/# psql -U postgres # psql (12.1 (Debian 12.1-1.pgdg100+1)) Type "help" for help.
Hey Postgres, connect to the database as the user, “postgres”
postgres=# CREATE DATABASE mytest; postgres=# \q
Alright, now create a database named “mytest”. Then quit.
We’re basically back in business. The new postgresql database has been created inside the docker container and my GraphQL node server should be able to connect to it via TypeORM. I just have to update my config file, or inline connection params.
default: type: "postgres" host: "localhost" port: 5432 username: "postgres" password: "mysecretpassword" database: "postgres" # plus more...
Figure out to save my local database data if my docker container is shutdown.
- see here
Weep over my broken relationship with RDS…
Get back to learning TypeORM. I wanted to figure out fetching paginated data, via UI scroll position. Probably query caching too.
Think about making a service that uses RDS, that is funded by community donations, or maybe Patreon patrons.
👋 I'm Kevin Wang.
Jazz Guitarist → Barista → Receptionist → Front End Engineer
This is my ever growing sandbox of sorts⛱.
You must log in to comment.
Be the first to comment!