... learning PostgreSQL basic usage

In this post we see how to use a PostrgreSQL database with Python.

I decided to use the dockerized version of PostgreSQL. I already have Docker installed on my PC. In case you need to install it, head over to Docker for Windows Docker desktop version

Step 1. Pull the image form the docker hub with

docker pull postgres

Step 2. Run the image docker

docker run --name test-db -e POSTGRES_PASSWORD=my_secret_password -d -p 5432:5432 postgres

A bit of explanations:

–name is the name of the container (not to confuse with the container ID)

-p is the port that is going to be open in the guest machine, port 5432 of the guest machine is forwarded to port 5432 of the host. Therefore I can connect to the guest just with localhost:5432

To run a bash console in the guest machine use this command:
docker exec -it <CONTAINER ID> bash

To get all the \ use the command:
docker container ls

In the bash run the psql application: psql -U postgres

Once you get the psgl prompt, you can use different commands:

- to list the database available: `\l`
- to list existing user: `\du`
- to list tables (also called relations): `\dt`

Leave a Reply

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>