|
|
||
|---|---|---|
| README.md | ||
How To Install And Set Up PostgreSQL - Docker Image
Installation
First, identify a port on which the database can be accessed. The convention is 5432, but let's say we pick 1337.
Then, choose a sufficiently secure password for the database admin account. Let's say you've picked "mysecretpassword" (which I beg of you, please do not actually pick).
You may also want to choose a name for the docker container - this is optional but very useful.
Then run the command:
docker run --name postgres-container -p 127.0.0.1:1337:5432 -v postgres-volume:/var/lib/postgresql -e POSTGRES_PASSWORD=mysecretpassword -d postgres
What does all of that mean?
docker run ... postgres means that you are taking an "image" - a fixed snapshot of a working system - called postgres (provided by Docker officially at hub.docker.com) and running it on your device as a docker container.
--name postgres-container means that you are naming the container "postgres-container". You can tweak this and choose whatever name you wish, and you can later use the same name to monitor the container.
-p 127.0.0.1:1337:5432 means that port 5432 on the container (where the PostgreSQL database is accessed from) can be accessed via (or "is forwarded to") port 1337 on the host computer, which is the device that is running the docker container - aka your computer. Additionally, it can only be accessed if the access is made to the 127.0.0.1 address. Your device can be part of many networks, and it has an address for each network. Specifying an IP address here makes this "port forwarding" work on only a specific network. 127.0.0.1 is the address that applications running on a computer use to access other applications on the same computer - it is not available to other computers. This means that the PostgreSQL database can only be accessed by other applications running on the same computer. If you would like any device to access the database, simply remove the IP address and run -p 1337:5432.
-v postgres-volume:/var/lib/postgresql is a volume bind. The /var/lib/postgresql folder in the container contains everything that your database stores. When you remove the container (why you would want to do this is explained below), you would lose everything within that folder. To prevent this, this command creates a permanent "volume" (basically just a folder with some extra information attached to it) on the computer called postgres-volume, and makes the container pretend that this volume is actually the /var/lib/postgresql folder.
-e POSTGRES_PASSWORD=mysecretpassword sets the password for the database admin account.
-d means "detach", which makes the container run in the background. Without it, the container would be running in the terminal, and you wouldn't be able to do anything else in the terminal without stopping the container.
You can stop the container with docker container stop postgres-container, and start it again with docker container start postgres-container. I don't know why you would want to do this but this is how you would do it if you wanted to.
If a new version of the Postgres container is released, to update your container, you would need to remove the existing container and create a new one.
To remove the container, first stop it, and then run docker container rm postgres-container. Then run the original command again, and it should download the latest image and run it.
Setup
Connect to the database by running the command
psql -U postgres -h 127.0.0.1 -p 1337
You may need to download psql on your system first. The -U postgres means that the username is postgres - the default name for the databasae admin. -h 127.0.0.1 means that the database is located at 127.0.0.1 (aka the same computer) - change this if the container is running on another computer. -p 1337 is the port that we've previously set up.
It will prompt you for your password, which you should enter.
First, run:
REVOKE ALL ON SCHEMA public FROM public;
to make sure that nothing is public.
Adding a new database
CREATE ROLE alice LOGIN ENCRYPTED PASSWORD 'alice_password';
CREATE DATABASE alice_db WITH OWNER=alice;.
REVOKE ALL ON DATABASE alice_db FROM public;.
These three steps first create an account, then create a database for that account (ideally, you would give each app its own account and database for separation of privileges), and then disallow others from even seeing basic information about the database.