Skip to content

Commit b920b9a

Browse files
committed
Add lab_1 skeleton
0 parents  commit b920b9a

8 files changed

+128
-0
lines changed

.editorconfig

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; top-most EditorConfig file
2+
root = true
3+
4+
; Windows-style newlines with spaces
5+
[*]
6+
indent_style = space
7+
indent_size = 4
8+
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.yml, Dockerfile]
13+
indent_size = 2
14+
15+
[Dockerfile]
16+
indent_size = 2
17+
18+
; enforce LF for Linux containers
19+
[*sh]
20+
end_of_line = lf
21+
indent_size = 2
22+
23+
[*sql]
24+
end_of_line = lf
25+
indent_size = 2

.gitattributes

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Always LF for bash scripts and SQL files
2+
*sh text eol=lf
3+
*sql text eol=lf

lab_1/README.md

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Lab 1 - Basic setup
2+
In this lab we wil create a simple PostgreSQL container and then try to connect to it.
3+
4+
## Start
5+
Run:
6+
```
7+
docker-compose up -d
8+
```
9+
10+
You can inspect `docker-compose.yml` file to see what exactly we're creating. It's very basic, but there are a couple of things to notice:
11+
* We're specifying that we want PostgreSQL 9.6.1 (at the time of this writing this is the latest version supported by AWS RDS), not just any version. Unless you're a Russian roulette enthusiast, always specify the exact version of the image that you want to use.
12+
* We map port 5432 on the container to the same port on the host. If you already have a PostgreSQL instance running on the host or are using this port in another container, this is not likely to work.
13+
```
14+
ERROR: for postgres Cannot start service postgres: driver failed programming external connectivity on endpoint lab1_postgres_1 (29743455530c605d8576f52c7880d9b653dfbfd7fe48bebbe2098306820f8e2a): Bind for 0.0.0.0:5432 failed: port is already allocated
15+
ERROR: Encountered errors while bringing up the project.
16+
```
17+
In this case just change the host port to a different value:
18+
```yaml
19+
ports:
20+
- "5433:5432"
21+
```
22+
You will need to specify this port explicitly to connect to Postgres server from the host.
23+
24+
## Inspect
25+
`docker-compose logs -f postgres`
26+
This will give a fair bit of output. Take note of this part:
27+
```
28+
postgres_1 | WARNING: No password has been set for the database.
29+
postgres_1 | This will allow anyone with access to the
30+
postgres_1 | Postgres port to access your database. In
31+
postgres_1 | Docker's default configuration, this is
32+
postgres_1 | effectively any other container on the same
33+
postgres_1 | system.
34+
postgres_1 |
35+
postgres_1 | Use "-e POSTGRES_PASSWORD=password" to set
36+
postgres_1 | it in "docker run".
37+
postgres_1 | ****************************************************
38+
```
39+
40+
Our database doesn't have a password for postgres user. We will fix this later, for now let's try connecting to it.
41+
42+
## Connect
43+
### psql
44+
psql is an essential command-line tool for managing PostgreSQL database.
45+
In a new console session get an interactive bash session inside the Postgres container:
46+
```
47+
docker-compose exec postgres bash
48+
```
49+
50+
Let's now try using psql:
51+
```
52+
psql
53+
```
54+
We will likely get the following output:
55+
```
56+
psql: FATAL: role "root" does not exist
57+
```
58+
Unless we specify a valid Postgres role, psql will try to use the currently logged in user and fail as there's no such role on the server. You will see the same message in the logs that are still streamed to the first console session.
59+
60+
Let's specify the user explicitly:
61+
```
62+
psql -U postgres
63+
```
64+
We're in and can take a look around:
65+
```
66+
\dt+
67+
```
68+
There's not much to see here now, so we can `Ctrl+D` out of this session for now.
69+
70+
### PgAdmin
71+
Right-click on 'Servers' and select 'Create > Server'.
72+
73+
![](img/pgadmin_create.png "Create server in PgAdmin")
74+
75+
Then specify the name of the server and connection parameters. We don't have a password, so we leave this field empty.
76+
77+
![](img/pgadmin_connect.png "Configure connection in PgAdmin")
78+
79+
### JetBrains DataGrip
80+
Connecting to a database is slightly less intuitive in DataGrip.
81+
* Press `Alt+1` to open the Database View
82+
* Press the green plus symbol, then select 'Data Source > PostgreSQL'
83+
84+
![](img/datagrip_create.png "Create server in DataGrip")
85+
* Fill in the details - make sure to specify the user (`postgres`) and download the missing Postgres drivers. Press 'Test Connection' to verify that everything is working.
86+
87+
![](img/datagrip_connect.png "Configure connection in DataGrip")
88+
89+
## Clean up
90+
This was a very basic, but functional PostgreSQL setup.
91+
Let's clean up the resources:
92+
```
93+
docker-compose down
94+
```

lab_1/docker-compose.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: '3'
2+
services:
3+
postgres:
4+
image: postgres:9.6.1
5+
ports:
6+
- "5432:5432"

lab_1/img/datagrip_connect.png

25.5 KB
Loading

lab_1/img/datagrip_create.png

18.5 KB
Loading

lab_1/img/pgadmin_connect.png

21.4 KB
Loading

lab_1/img/pgadmin_create.png

13.2 KB
Loading

0 commit comments

Comments
 (0)