Odoo Development: Running PostgreSQL in Docker
Welcome to the first in a series of articles on getting started with developing business apps with the Odoo framework, the fastest way to build highly functional business applications, without reinventing the wheel.
Odoo relies on PostgreSQL, the worlds most advanced Open Source relational database engine, for its data storage. This article describes how to run a PostgreSQL database server on your laptop using Docker.
What is Docker?
Docker is technology that allows you to package up applications in a way that will let them run on any operating system. Kinda like Virtual Machines, but way more lightweight!
Why run PostgreSQL in Docker for development?
- You don’t have to install it!
- You can run different versions on the same machine easily
- It runs in the foreground so you can monitor it without trawling through log files
- You’ll be running the Linux version of PG, which means you are less likely to run into issues restoring backups from your Linux servers
How do I get started?
- Create a DockerHub account [sigh]
- Download Docker Desktop and install it!
In order to make launching the docker image easy, we’ll be writing a simple Bash script. On Mac and Linux, you are already set up. If you are on Windows, then I you can download Git for Windows which includes “Git Bash”.
Creating the PostgreSQL Start Script
Once you have Docker installed, you have everything you need to start your PostgreSQL server. If you have a photographic memory, you could even skip writing the script below, but personally I like to have my computer remember the commands instead of typing them :)
In any folder, create a file named pg11.sh, and add the code below:
If you are on Windows, in your text editor make sure this file has “Line Feed” (LF) line endings.
Run it!
To run it, go into your terminal (or on Windows open Git Bash) and run it:
./pg11.sh
Your PostgreSQL 11 Database Server is Served! :)
You should now be able to connect to it using a database admin tool like DBeaver, on localhost port 5432, using username “postgres” and the password you put in the shell script above.
When you are finished with it, press CTRL+C and it will stop.
Persisting Data
Any users and databases you create will be saved between runs, thanks to the -v postgres11-data argument in the script above, which creates a “Docker Volume” (kinda like a virtual hard disk).
You can delete your PostgreSQL data simply using:docker volume rm postgres11-data
if you want to get rid of it.
Running Multiple Versions
You can run multiple PostgreSQL versions simply by creating copies of the script above, and modifying the version numbers accordingly.
Note that if you want to run multiple versions at once, you’ll need to give them different local port numbers. To do this, change:-p 5432:5432
in the script above to, e.g.-p 5555:5432
which will run the second database server on port 5555 instead of 5432