|
| 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 | + |
| 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 | + |
| 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 | + |
| 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 | + |
| 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 | +``` |
0 commit comments