Skip to content

Commit 55af0ea

Browse files
committed
Initial commit
0 parents  commit 55af0ea

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

00-postgres-init.sh

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# /bin/sh
2+
3+
# create directory if does not exist
4+
if [ -d "$HOME/docker/volumes/postgres" ]
5+
then
6+
echo "Directory $HOME/docker/volumes/postgres exists."
7+
else
8+
echo "Error: Directory $HOME/docker/volumes/postgres does not exists."
9+
mkdir -p $HOME/docker/volumes/postgres
10+
fi
11+
12+
# launch the postgres image called 'post_setup',
13+
# attach it to the local volume
14+
docker run --rm --name post_setup \
15+
-e POSTGRES_USER=postgres \
16+
-e POSTGRES_PASSWORD=docker \
17+
-d \
18+
-p 5432:5432 \
19+
-v $HOME/docker/volumes/postgres:/var/lib/postgresql/data \
20+
postgres:13.3
21+
22+
# create a new role, and two databases
23+
echo "CREATE ROLE rahul WITH PASSWORD 'pass' CREATEDB LOGIN;
24+
CREATE DATABASE work; CREATE DATABASE anomaly;" | \
25+
docker exec -i post_setup \
26+
psql -U postgres
27+
28+
# stop the docker container
29+
docker stop post_setup

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# How to setup a Postgres from scratch
2+
3+
1. Pull postgres docker image using `docker pull postgres:13.3`
4+
1. Edit `00-postgres-init.sh` with your custom settings
5+
1. Run `./00-postgres-init.sh`, this sets up new roles + new databases
6+
1. Check the configuration in `docker-compose.yml` for your postgres database settings and docker images
7+
1. To run RStudio and Postgres together, run `docker-compose up -d`
8+
1. Connect to RStudio in a browser using `localhost:8787`
9+
1. Test your database by running `postgres.R`

docker-compose.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: "3.3"
2+
services:
3+
db:
4+
image: postgres:13.3
5+
restart: unless-stopped
6+
environment:
7+
POSTGRES_DB: "anomaly"
8+
POSTGRES_USER: "rahul"
9+
POSTGRES_PASSWORD: "pass"
10+
ports:
11+
- "5432:5432"
12+
volumes:
13+
- type: "bind"
14+
source: "$HOME/docker/volumes/postgres"
15+
target: "/var/lib/postgresql/data"
16+
rstudio:
17+
image: hatmatrix/blog:base
18+
ports:
19+
- "8787:8787"
20+
- "3838:3838"
21+
environment:
22+
DISABLE_AUTH: "true"
23+
volumes:
24+
- type: "bind"
25+
source: "$HOME/github"
26+
target: "/home/rstudio"
27+
depends_on:
28+
- "db"

postgres.R

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Quick script to check if things are working fine
2+
3+
library(DBI)
4+
library(magrittr)
5+
6+
con <- DBI::dbConnect(
7+
drv = RPostgres::Postgres(),
8+
dbname = "anomaly",
9+
host = "db", # this needs to be the name of the postgres service
10+
# (line 3 in docker-compose.yml)
11+
user = "rahul",
12+
password = "pass",
13+
port = 5432
14+
)
15+
16+
DBI::dbWriteTable(con, "mtcars", mtcars, overwrite = T)
17+
18+
con %>%
19+
DBI::dbListTables()
20+
21+
con %>%
22+
dplyr::tbl("mtcars")
23+
24+
con %>%
25+
DBI::dbListFields("mtcars")
26+
27+
# ---

0 commit comments

Comments
 (0)