SQL Prototyping with Docker
2021-01-12
2 minutes
So recently I ran into a bit of a snafu. I wanted to do some SQL prototyping (with postgres) on my local machine but didn’t have the appropriate permissions to install and mess around with it. The postgres Docker image really came in handy. Below are some notes about what I did to get up and running with postgres in Docker. I thought they might come in handy to a Data Scientist or Machine Learning engineer facing a similar situation.
Once you have Docker up and running on your machine you just have to run a container of the postgres image from the command line.
docker run -e POSTGRES_USER=foo -e POSTGRES_PASSWORD=foo -d postgres
Then you can bash into the container. You can find the container name by running docker ps
.
docker exec -it <container name> /bin/bash
Now we’re in the container but we need to switch user (postgres prevents you from doing pretty much anything as root)
useradd foo
su - foo
Finally, we can launch psql
(the terminal interface to postgres). Your prompt should now start with foo=#
. Now we can quickly create tables and execute queries.
CREATE TABLE synthetic (id VARCHAR(1), var1 INT, var2 INT);
INSERT INTO synthetic (id, var1, var2)
VALUES ('A', 1, 1), ('B', 1, 0);
SELECT * FROM synthetic;
id | var1 | var2
----+------+------
A | 1 | 1
B | 1 | 0
foo=# \dt
List of relations
Schema | Name | Type | Owner
--------+-----------+-------+-------
public | synthetic | table | foo
(1 row)
Don’t forget to shutdown the container with docker stop <container name>
when you’re done.