Setting up a test database

To create our database, you can use Docker, which automatically pulls all the necessary layers of the images containing the preinstalled PostgreSQL database. It's important to note that PostgreSQL has official images on Docker Hub, and you should opt to use these instead of unofficial ones, because the latter have a greater risk of malicious updates.

We need to start a container with a PostgreSQL database instance. You can do this using the following command:

docker run -it --rm --name test-pg -p 5432:5432 postgres

What does this command do? It starts a container from the postgres image (the latest version) and uses port 5432 on the localhost to forward it to the inner port, 5432, of the container (that is, the port exposed by the image). We also set a name with the --name argument. We give the container the name test-pg. You can use this name later to stop the container. The --rm flag will remove the anonymous volumes associated with the container when it's stopped. So that we can interact with the database from a Terminal, we've added -it flags.

The database instance will start and print something like the following to the Terminal:

creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
........

The database is now ready for use. You can check it with the psql client, if you have it locally. The default parameters of the image are as follows:

psql --host localhost --port 5432 --username postgres

If you don't need the database anymore, you can use the following command to shut it down:

docker stop test-pg

But don't shut it down yet—let's connect to it with Rust.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.178.9