Skip to content

Commit 1e4be6e

Browse files
committed
tweak psql
1 parent 54ced94 commit 1e4be6e

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

src/content/13/en/part13a.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ The reason why the the previous sections of the course used MongoDB is precisely
6767

6868
### Application database
6969

70-
For our application we need a relational database. There are many options, but we will be using the currently most popular Open Source solution [PostgreSQL](https://www.postgresql.org/). You can install Postgres (as the database is often called) on your machine, if you wish to do so. An easier option would be using Postgres as a cloud service, e.g. [ElephantSQL](https://www.elephantsql.com/). You could also take advantage of the course [part 12](/en/part12) lessons and use Postgres locally using Docker.
70+
For our application we need a relational database. There are many options, but we will be using the currently most popular Open Source solution [PostgreSQL](https://www.postgresql.org/). You can install Postgres (as the database is often called) on your machine, if you wish to do so. An easier option would be using Postgres as a cloud service, e.g. [ElephantSQL](https://www.elephantsql.com/).
7171

7272
However, we will be taking advantage of the fact that it is possible to create a Postgres database for the application on the Fly.io and Heroku cloud service platforms, which are familiar from the parts 3 and 4.
7373

7474
In the theory material of this section, we will be building a Postgres-enabled version from the backend of the notes-storage application, which was built in sections 3 and 4.
7575

76+
Since we don't need any database in the cloud in this part (we only use the application locally), there is a possibility to use the lessons of the course [part 12](/en/part12) and use Postgres locally with Docker. After the Postgres instructions for cloud services, we also give a short instruction on how to easily get Postgres up and running with Docker.
77+
7678
#### Fly.io
7779

7880
Let us create a new Fly.io-app by running the command _fly launch_ in a directory where we shall add the code of the app. Let us also create the Postgres database for the app:
@@ -130,6 +132,33 @@ Type "help" for help.
130132
postgres=#
131133
```
132134

135+
#### Docker
136+
137+
This instruction assumes that you master the basic use of Docker to the extent taught by e.g. [part 12](/en/part12).
138+
139+
Start Postgres [Docker image](https://hub.docker.com/_/postgres) with the command
140+
141+
```bash
142+
docker run -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 postgres
143+
```
144+
145+
A psql console connection to the database can be opened using the _docker exec_ command. First you need to find out the id of the container:
146+
147+
```bash
148+
$ docker ps
149+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
150+
ff3f49eadf27 postgres "docker-entrypoint.s…" 31 minutes ago Up 31 minutes 0.0.0.0:5432->5432/tcp great_raman
151+
docker exec -it ff3f49eadf27 psql -U postgres postgres
152+
psql (15.2 (Debian 15.2-1.pgdg110+1))
153+
Type "help" for help.
154+
155+
postgres=#
156+
```
157+
158+
Defined in this way, the data stored in the database is persisted only as long as the container exists. The data can be preserved by defining a
159+
[volume](/en/part12/building_and_configuring_environments#persisting-data-with-volumes) fort the data, see more
160+
[here](https://github.com/docker-library/docs/blob/master/postgres/README.md#pgdata).
161+
133162
#### Using the psql console
134163

135164
Particularly when using a relational database, it is essential to access the database directly as well. There are many ways to do this, there are several different graphical user interfaces, such as [pgAdmin](https://www.pgadmin.org/). However, we will be using Postgres [psql](https://www.postgresql.org/docs/current/app-psql.html) command-line tool.
@@ -291,8 +320,7 @@ $ cat .env
291320
DATABASE_URL=postgres://<username>:<password>@ec2-54-83-137-206.compute-1.amazonaws.com:5432/<databasename>
292321
```
293322

294-
When using Fly.io, the local connection to the database should first be enabled by
295-
[tunneling](https://fly.io/docs/reference/postgres/#connecting-to-postgres-from-outside-fly)
323+
When using Fly.io, the local connection to the database should first be enabled by [tunneling](https://fly.io/docs/reference/postgres/#connecting-to-postgres-from-outside-fly)
296324
the localhost port 5432 to the Fly.io database port using the following command
297325

298326
```bash
@@ -318,6 +346,12 @@ Password was shown when the database was created, so hopefully you have not lost
318346

319347
The last part of the connect string, <i>postgres</i> refers to the database name. The name could be any string but we use here <i>postgres</i> since it is the default database that is automatically created within a Postgres database. If needed, new databases can be created with the command [CREATE DATABASE](https://www.postgresql.org/docs/14/sql-createdatabase.html).
320348

349+
If you use Docker, the connect string is:
350+
351+
```bash
352+
DATABASE_URL=postgres://postgres:mysecretpassword@localhost:5432/postgres
353+
```
354+
321355
Once the connect string has been set up in the file <i>.env</i> we can test for a connection:
322356

323357
```bash

src/content/13/fi/osa13a.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ Syy sille miksi kurssin aiemmat osat käyttivät MongoDB:tä liittyvät juuri se
6666

6767
### Sovelluksen tietokanta
6868

69-
Tarvitsemme sovellustamme varten relaatiotietokannan. Vaihtoehtoja on monia, käytämme kurssilla tämän hetken suosituinta Open Source -ratkaisua [PostgreSQL:ää](https://www.postgresql.org/). Voit halutessasi asentaa Postgresin (kuten tietokantaa usein kutsutaan) koneellesi. Helpommalla pääset käyttämällä jotain pilvipalveluna tarjottavaa Postgresia, esim. [ElephantSQL:ää](https://www.elephantsql.com/). Voit myös hyödyntää kurssin [osan 12](/en/part12) oppeja ja käyttää Postgresia paikallisesti Dockerin avulla.
69+
Tarvitsemme sovellustamme varten relaatiotietokannan. Vaihtoehtoja on monia, käytämme kurssilla tämän hetken suosituinta Open Source -ratkaisua [PostgreSQL:ää](https://www.postgresql.org/). Voit halutessasi asentaa Postgresin (kuten tietokantaa usein kutsutaan) koneellesi. Helpommalla pääset käyttämällä jotain pilvipalveluna tarjottavaa Postgresia, esim. [ElephantSQL:ää](https://www.elephantsql.com/).
7070

71-
Käytämme nyt kuitenkin hyväksemme sitä, että osista 3 ja 4 tuttuille pilvipalvelualustoille Fly.io ja Heroku on mahdollista luoda sovellukselle Postgres-tietokanta.
71+
Käytämme nyt hyväksemme sitä, että osista 3 ja 4 tuttuille pilvipalvelualustoille Fly.io ja Heroku on mahdollista luoda sovellukselle Postgres-tietokanta.
7272

7373
Tämän osan teoriamateriaalissa rakennetaan osissa 3 ja 4 rakennetun muistiinpanoja tallettavan sovelluksen backendendistä Postgresia käyttävä versio.
7474

75+
Koska emme tarvitse tässä osassa mihinkään pilvessä olevaa tietokantaa (käytämme sovellusta ainoastaan paikallisesti) on eräs mahdollisuus hyödyntää kurssin [osan 12](/en/part12) oppeja ja käyttää Postgresia paikallisesti Dockerin avulla. Pilvipalveluiden Postgresohjeiden jälkeen annamme myös lyhyen ohjeen miten Postgresin saa helposti pystyn Dockerin avulla.
76+
7577
#### Fly.io
7678

7779
Luodaan nyt sopivan hakemiston sisällä Fly.io-sovellus komennolla _fly launch_ ja luodaan sovellukselle Postgres-tietokanta:
@@ -124,6 +126,33 @@ Type "help" for help.
124126
postgres=#
125127
```
126128

129+
#### Docker
130+
131+
Tämä ohje olettaa, että hallitset Dockerin peruskäytön esim. [osan 12](/en/part12) opettamassa laajuudessa.
132+
133+
Käynnistä Postgresin [Docker image](https://hub.docker.com/_/postgres) komennolla
134+
135+
```bash
136+
docker run -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 postgres
137+
```
138+
139+
Tietokantaan saadaan psql-konsoliyhteys komennon _docker exec_ avulla. Ensin tulee selvittää kontainerin id:
140+
141+
```bash
142+
$ docker ps
143+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
144+
ff3f49eadf27 postgres "docker-entrypoint.s…" 31 minutes ago Up 31 minutes 0.0.0.0:5432->5432/tcp great_raman
145+
docker exec -it ff3f49eadf27 psql -U postgres postgres
146+
psql (15.2 (Debian 15.2-1.pgdg110+1))
147+
Type "help" for help.
148+
149+
postgres=#
150+
```
151+
152+
Näin määriteltynä tietokantaan talletettu data sailyy ainoastaan niin kauan kontti on olemassa. Data saadaan säilymään määrittelemällä datan talletukseen
153+
[volume](/en/part12/building_and_configuring_environments#persisting-data-with-volumes), katso lisää
154+
[täältä](https://github.com/docker-library/docs/blob/master/postgres/README.md#pgdata).
155+
127156
#### psql-konsolin käyttöä
128157

129158
Erityisesti relaatiotietokantaa käytettäessä on oleellista päästä tietokantaan käsiksi myös suoraan. Tapoja tähän on monia, on olemassa mm. useita erilaisia graafisia käyttöliittymiä, kuten [pgAdmin](https://www.pgadmin.org/). Käytetää nnyt kuitenkin Postgresin [psql](https://www.postgresql.org/docs/current/app-psql.html)-komentorivityökalua.
@@ -303,6 +332,12 @@ DATABASE_URL=postgres://postgres:<password>@localhost:5432/postgres
303332
304333
Salasana on se, jonka on otettu talteen tietokantaa luodessa.
305334
335+
Dockeria käytettäessä connect string on:
336+
337+
```bash
338+
DATABASE_URL=postgres://postgres:mysecretpassword@localhost:5432/postgres
339+
```
340+
306341
Connect stringin viimeinen osa <i>postgres</i> viittaa käytettävään tietokannan nimeen. Nyt se on valmiiksi luotava ja oletusarvoisesti käytössä oleva <i>postgres</i>-niminen tietokanta. Komennolla [CREATE DATABASE](https://www.postgresql.org/docs/14/sql-createdatabase.html) on tarvittaessa mahdollista luoda muita tietokantoja Postgres-tietokantainstanssiin.
307342
308343
Kun connect string on määritety tiedostoon <i>.env</i> voidaan kokeilla muodostuuko yhteys:

0 commit comments

Comments
 (0)