Deploy your PostgreSQL Database in a Docker container on a NAS Synology
In this story I explain how to deploy a PostgresSQL Database on your NAS Synology with Docker, for any needs such:
- Have a database for your website. I wrote an other article that explain how to deploy your website on your NAS using Shiny / Golem and Docker (https://medium.com/@julienmilon/deploy-your-website-using-golem-shiny-docker-on-nas-synology-1b4df0e9cbd4), with those 2 articles you could have your own stucture web with a website and a database.
- Have a database to store your own data on your server.
Prerequisites for this story :
- Have some notion of what is Docker (you can find some notion around Docker in my previous story https://medium.com/@julienmilon/deploy-your-website-using-golem-shiny-docker-on-nas-synology-1b4df0e9cbd4).
- Have your own NAS, especially a Synology to have the same configuration than this story, neverless with other brands the process should be very similar.
Build your Docker container with PostgreSQL Database
Charge postgis/postgis image on Docker
https://registry.hub.docker.com/r/postgis/postgis/
- Open Docker on your NAS Synology.
- Go on Repository Tab and search postgis/postgis.
- Select and charge the version that you want.
Now you should see the image on Images Tab.
Run your Docker container with postgis/postgis image
- Double click on postgis/postgis Image
- Select Bridge Network
- Name your container.
- Give a limite of space if you want it. Optional
- Select “Enable automatic restart”.
- Click on “Advanced Parameters” to define your password and your username
- Add 2 variables, POSTGRES_USER and POSTGRES_PASSWORD and define their value. For me POSTGRES_USER did not work, and the default user is postgres. The USER is maybe in Run command Tab but I did not try it
- Save
- Define the port that you want to use. Do not 5432 and other used port.
- To save your data in your NAS Synology and not loss it when the container stop or restart define a Share volume. The first folder is the folder on your NAS Synology Files and add in “Access path” /var/lib/postgresql/data
- Execute
You may need to define a rule on your internet Box to to root to your Synology NAS if you connect to the chosen port on your internet box.
Public IP address : port — > NAS Synology : port
Get access to your database with Pgadmin 4
- Download pgAdmin 4 on your computer (https://www.pgadmin.org/download/pgadmin-4-windows/ for Windows).
- If it the first time that you use it define the password of your pgAdmin.
- Click right on “Server”, than click on “Register — > Server…”
- Define the name that you want for this server connection.
- Go on “Connection” Tab to add your Database information.
- Host is your IP Public address
- Port is the port that your used on your Docker container
- Maintenance database and Username are the User that you add on the container parameters. By default it is “postgres”
- Add your Password that you define with POSTGRES_PASSWORD
- And Save
You now have access to your Database with pgAdmin. You can also connect with any programming language and their IDE such with R programming language and RStudio or Python with Jupiter Notebook or PyCharm ,...
In connection with another story that I wrote (https://medium.com/@julienmilon/deploy-your-website-using-golem-shiny-docker-on-nas-synology-1b4df0e9cbd4), you could use this database to connect it to a website that you have or want to deploy on your Synology NAS, to have application logs, in user bases or even in data sources …