Working with PostGreSQL and PostGIS in Docker + QGIS
Using Docker for databases
I’ve been wanting to explore more possibilities in databases and spatial data since working on a large data project during my PhD. Since then I really haven’t had data that would warrant the effort needed to design and setup a database. Recently I was reminded about Docker, which I have used to spin up Rstudio instances before. After some internet work I found some handy sources for running databases in containers and so I thought I would try it out as a platform for learning more.
Here is my effort to get a database up and running in a container and then connect to said database with Dbeaver and then QGIS.
Download and install Docker; Download and install Dbeaver Community Edition; Download and install QGIS
- Get a PostGreSQL database running in a container
- At the terminal type:
docker container run -d --name=pg -p 5432:5432 -e POSTGRES_PASSWORD=docker -e PGDATA=/pgdata -v /pgdata:/pgdata postgres
- This will download (if it doesn’t already exist on your machine) the image from docker and start it up. You will now have running a container with a PostGres database active.
- Some explanation of the command
-d
means the container will run in the background-name=pg
names the container, rather than docker assigning a random name-p 5432:5432
assigns port 5432 of the container to 5432 on the host. You will connect to the PostGres database through this port-e POSTGRES_PASSWORD=docker
is an environment variable for PostGres, it sets the password to docker in this case-e PGDATA=/pgdata
sets the data location for the database in the container to the folder/pgdata
. This folder will be created in the container.-v /pgdata:/pgdata
sets a volume connection from inside the container to a location on the host machine. This allows you to persist your database data when the container is stopped or even deleted.postgres
is the image we will download, this will be the latest version of PostGreSQL
- This comes from www.youtube.com/watch?v=iZDbENJrl4I
- Another option for the persistence data is just to assign a folder on the host to the default data location inside the container. It may look like
-v /[local folder]:/var/lib/postgresql/data
- I found that the
/pgdata
command above persisted but I don’t know where it lived on the host machine. It persisted between stopping the container but disappeared after a Docker app update, YMMV.
- I found that the
- If you open the Docker dashboard you will see the container running, success!
- At the terminal type:
- Connect from the command line
- You can connect to the container and get to command line inside of it by executing a command
docker container exec -it pg bash
- This brings up bash inside of the container, which is a Linux machine.
-it
makes it interactive so that you can type into itpg
is the name of our container &bash
is the command- You can type
ls
to see the contents, which include the folder/pgdata
. Typeexit
- Use
psql
to make a table- Access
psql
inside of the containerdocker container exec -it pg psql -U postgres
-U postgres
is the user that you use to access the database. Since you are already inside of the container to connect, no password is needed.- Create a table at the prompt
postgres=#
create table table1(name text, id int generated by default as identity);
- Then insert some data
insert into table1(name) values('PostGreSQL'), ('database'), ('Dbeaver');
- Now you have a table in your database that is running within a docker container. Confirm it by typing
\dt
- Access
- You can connect to the container and get to command line inside of it by executing a command
- Connect to database with Dbeaver
- If you open Dbeaver you can connect to this running database by setting up a PostGreSQL connection. The host will be
localhost
Database will bepostgres
Username will bepostgres
and the password is the one set in the docker command, in this casedocker
- Test the connection with the button, then Finish
- You should see our
table1
under postgres>Schemas>public>Tables - To use Dbeaver check out the documentation.
- If you open Dbeaver you can connect to this running database by setting up a PostGreSQL connection. The host will be
- Get PostGreSQL + PostGIS running in a container
- For this I use
docker run --name=pgdb -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -e PGDATA=/pgdata -d -v gisdata:/pgdata postgis/postgis
- This uses a named volume managed by Docker at
gisdata
It doesn’t already exist so Docker will create it. You can check these withdocker volume ls
- For this I use
- Connect to database with QGIS
- This can be done by right clicking on the PostGIS in the Browser. Once connected you can mange the connection in the DB Browser and do things like import data into the database. Probably best to update to 3.10 at least.
There was a fair amount of Googleing to explore all of this. I had issues arise around connecting to volumes. It is nice to be able to persist data so that you could use this process for learning and analysis. Docker is a good way to keep the environment stable and isolated from whatever machine you are on (I was working on Windows 10 and MacOS).