Setting up PostgreSQL database with a Django Docker application
ฝัง
- เผยแพร่เมื่อ 25 พ.ย. 2024
- In this video, I’ll show you how to set up a PostgreSQL database with a Django Docker application. This video follows from my first video Dockerizing a Django REST Framework Project: • Dockerizing a Django R...
Docker: www.docker.com
Docker-Compose: docs.docker.co...
GitHub Repo: github.com/Lon...
The course I refer to in the video is Build Your Own Backend REST API using Django REST Framework Course: www.udemy.com/...
OTHER COURSES
• Build a Backend REST API with Python & Django, Django REST Framework and Docker using Test Driven Development (TDD) ADVANCED: www.udemy.com/...
• DevOps Deployment Automation with Terraform, AWS and Docker: londonappdevel...
VIDEOS YOU MIGHT LIKE:
○ Prepare a Django app for Deployment using Docker - • Prepare a Django app f...
○ BUILD A BACKEND REST API with Python, Django REST Framework, Django, Vagrant & VirtualBox - • Build a BEGINNER backe...
○ BUILD A BACKEND REST API with Python, Django & Test Driven Development - • Build an ADVANCED back...
○ CREATING A DJANGO PROJECT with Docker - • CREATING A DJANGO PROJ...
○ Dockerizing a Django REST Framework Project - • Dockerizing a Django R...
○ THE 4 BEST WAYS TO DEPLOY A DJANGO APPLICATION - • The 4 best ways to dep...
Question wouldn’t all your previous data be lost as in the data you saved before you plugged in to the new database
Great but i was also expecting the pg_admin setup and also change user for postgres and password
You need to update in the requirements.txt Django==1.11.17 (or more), otherwise there is failure when building.
Thank you
Hello I'm facing an error:
django.db.utils.operationalerror could not translate host name "db" to address: Temporary failure in name resolution
I tried by adding links: and network: in docker compose file, but still it's showing the same
Try this.
db:
image: postgres
environment:
POSTGRES_HOST_AUTH_METHOD: trust
@@minhajuddinansari561 Wow that helped a lot, thank you !
i din't change anything but docker compose fails saying can't open file 'src/profiles_project/manage.py': [Errno 2] No such file or directory
Same issue here
Replace the manage.py path to `./src/profiles_project/manage.py` when you are running migrations as well as the web server command in the docker-compose.yml file. Worked for me!
@London App Developer. So but it is better to omit de db container in production? Because I have read that it is not good to put a database in a docker container
Perfect , I have this problem. How to deploy this site not to localhost but hosting on a service like Google cloud Run.
I am trying to deploy a site like this on a platform like digital Ocean or GCP but doesn't work.
And more important, if I deploy this website where is the permanent storage?
If I deploy my site on localhost the real database is on :
volumes:
- ./data/db:/var/lib/postgresql/data
./data/db
but when it's deployed on a extern platform , where are effectly stored the data ?
Thanks a lot if you can tell me the answer
All the best regards
Super useful. That just worked with my old project. Thank you, Mark!
Exactly what I needed. Thanks!
Splendid.
loved your video, but im getting this error when i ran migrate commad
django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known
same
Maybe try running `docker-compose down` followed by `docker-compose up`... It could be that the database didn't start in time for some reason.
environment:
- "POSTGRES_HOST_AUTH_METHOD=trust" in db service solved this for me
Hi, I am in need of a tutorial for deploying a Django Docker app to Azure which will point to Azures PostgreSQL resource. Do you have that course?
When running docker-compose up, I get
web_1 | python: can't open file 'src/profiles_project/manage.py': [Errno 2] No such file or directory
byob-profiles-rest-api-docker_web_1 exited with code 2
even if my manage.py is in
D:\...\byob-profiles-rest-api-docker\src\profiles_project
how is this possible?
It might be fixed using python:3.6.12 version of python in the Dockerfile.
You are my superhero. Saved a lot of my time
So now, how to connect to those databases in postgres container through pgadmin for backup, install plugins etc .. ?
there is an image for pgadmin you can configure it to run as a new image and bind it to a different port
give me a second and ill show you how it is done
Here is the image link
hub.docker.com/r/dpage/pgadmin4
Here is the code to set it up and running!
pgadmin4:
image: dpage/pgadmin4
ports:
- 8050:80
environment:
PGADMIN_DEFAULT_EMAIL: postgres@localhost
PGADMIN_DEFAULT_PASSWORD: password
How do you deploy this to a VPS?
How can you access to postgresql outside the container now? jus like a normal django app because now the host is 'db' .
I just wanted to start learning Docker ... thanks, for this great intro!
You're welcome Matej! Glad you found it useful.
Everything looks so smooth, but if it comes to publish my own project I get one error after another. I just go one by one but this one drives my crazy:
django.db.utils.OperationalError: could not translate host name "dummy_endpoit" to address: Name does not resolve
Awesome, discovered just in time to migrate my db from sqlite to postgresql. Still stuck where docker spit out no such file or directory to run migrate command.
Could you perhaps also make a video to demonstrate how can we populate this freshly created database in the container with the data in existing database? I had an existing Django rest project set up with MySQL which already had some data saved. I decided to dockerize mmy project and followed both your tutorials. I can access my application at localhost.
Now, I really would like to see how I could populate this database in the docker container with the data in my local MySQL database.
Hey Nitish, thanks for the comment, I'll definitely keep this in mind when making future content.
Worked perfectly! Thanks mate.
how can you access that database? when I add network_mode: bridge it fails building
Everything went well as per the tutorial . only when I run migrate I hit in to a trouble with psycopg2. Any ideas or suggestion? django core exception .Improperly configured: Error loading psycopg2 module :No module named 'psycopg2'
Double check that this dependency is in your requirements.txt and try running `docker-compose build` again
psycopg2 ---> IMHO pronounced "psycho pg two"
LOL
Excellent demo. Thanks!
can you please do it with the mysql as well
Hi , I have this eror when type: sudo docker-compose up
Building web
Step 1/7 : FROM python:3
ERROR: Service 'web' failed to build: Get registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on [::1]:53: read udp [::1]:56049->[::1]:53: read: connection refused
Please help me on this.
thanks for the tutorial, but i got `django.db.utils.OperationalError: FATAL: password authentication failed for user "postgres"`
I had a similar problem. Check to see if your database specifications in local 'settings.py' has the 'PASSWORD' field. If not, you'll need to define one (simply add 'PASSWORD': 'postgres' for now)
@@36plain54 thank you
Amazing he was able to run a PG image without a PG password.
I'm getting this error -
web_1 | Is the server running on host "localhost" (127.0.0.1) and accepting
web_1 | TCP/IP connections on port 5432?
web_1 | could not connect to server: Cannot assign requested address
web_1 | Is the server running on host "localhost" (::1) and accepting
web_1 | TCP/IP connections on port 5432?
Try stopping and starting the docker containers?
How do you migrate the actual data in tables from previous sqlite to postgresql? I did't see this in your video.
Hey yes that's not covered in the video. This would not be straightforward unfortunately. The easiest way might be to write a script that exports the tables to CSV, then write another script to import them from CSV files into PostgresSQL.
Hi London, excellent video and very clear. I've followed all your steps exactly however I'm still running into the same error which brought me to your video; any call to manage.py or attempts to run the app with docker run into the following error:
- django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known
any idea why this might be?
running on a linux server with:
docker version == 18.03.1-ce
django version == 1.11.1
Hi I got the same error, how did you fix it?
Try this.
db:
image: postgres
environment:
POSTGRES_HOST_AUTH_METHOD: trust
I have problems with the repository that you uploaded, at the moment of raising docker compose, it marks the following error: --WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or` docker-compose up --build`-- and it does not allow to create the Django service, I tried to do it in playground and it also sent me an error
Is it a warning or an error? Warnings typically do t prevent it from working, they just let the dev know something might need to be fixed or changed in the future.
Hello, I have created django app and connect to Postgres. But I need help on some .. could I send the document that I need help.
Sorry but I don't have capacity to offer one to one debugging... I recommend posting on Stack Overflow or asking on community IRC.
Great video! I would love your advice. I have a django app which has a
mysql db which I need to change over to postgres. Can I simply make the
changes in the docker-compose.yml file or do I need to search for any
mysql reference thoughout the whole app and change it to psql? Thank you
for your help, I can't find much info about it online.
Thanks! That depends... Is there a requirement to migrate data over or just create a fresh DB in PostgreSQL? If you need to migrate data, you'll probably need to run some SQL exports or potentially create a custom script for exporting/importing data. Otherwise you should be good to just switch the MySQL service to use Postgres and update your database backend in Django. Also, you would need to check your code for any hard dependencies on MySQL (for example, specific MySQL fields used in models, raw queries, etc...).
docker-compose up fails with'Error: Service 'web' failed to build unexpected EOF' any help would be appreciated. great vid.
Cool Video.. I was looking for this one. I wish you could have included where to find the data in postgres container.
HI, first you start your containers with docker-compose up. Then do docker ps -a . Then you will see the running containers. Then you will do a: docker exec -it nameContainer bash. Then you have to switch to the roler from your database: psql -U roleDatabase. And then with \d+ you will see all the tables in that database. and you can do just a select*from tableName and see all the data from that table.
Thank you brother!
What is the version of Django you are using here?
Quite an old version (1.11) but the steps should be the same for Django 3
Why are you in bieszczady mountains :O?
The photo is from Banff National Park (Canada).
it totally work, amazing, thanks a lot, I will buy your udemy courses for sure.
Fantastic, thanks José.
i can't find my database in pgadmin there is no server and no database everything is empty why please ?
Unfortunately it's difficult to tell without seeing it. If the Django project runs (and migrations apply) then the data must be somewhere.
Thank you 🎉
I think psycopg2 updated to psycopg2-binary
No those are different things. The -binary one is precompiled. I understand it's better to compile it so it's optimised for the OS.
If you're watching this video and getting errors with the docker-compose command, you need to check this thread: stackoverflow.com/questions/51959890/docker-compose-and-django-generator-syntax-error/54437379#54437379?newreg=752e0329860c42699469069618b03fea
"Your Django version is not compatible with Python 3.7
So make these changes
In your Dockerfile
FROM python:3.6
Restart docker using sudo service docker restart
then sudo docker-compose up --build
or sudo docker-compose run web python manage.py migrate and sudo docker-compose up --build"
Also, if you have difficulty stopping your docker containers after starting this project:
stackoverflow.com/questions/28069240/unable-to-stop-or-remove-a-container-in-docker-permission-denied-is-displayed
linuxize.com/post/how-to-remove-docker-images-containers-volumes-and-networks/
This worked for me, but please, do read the articles, I'm a complete noob:
"sudo aa-remove-unknown
sudo service docker restart
sudo docker container stop $(docker container ls -aq)"
Hi, can you create a Django using a mySQL database app into a Docker container. Great videos! Thank you.
Should be more or less exactly the same, except you will use a MySQL docker image and configure Django for a mysql backend.
Hey London! Thanks a lot for upload this video! I have a issue... I does all of this video but when I run python manage.py migrate, this still save all information in db.sqlite3 and not in my PostgreSQL container... Someone has the same issue than can help me??
It helped me me a lot!
Took your rest api course. Was pretty boring but probably best course I have done. Help me get a ton of important skills and got lots of oppertunities. thank you! _/\_
Lol thank you for taking our course. Sorry it was boring but glad to hear the boredom paid off in the end :)
docker app does not start...
many many thanks!
Most welcome!
i dont know but when i make `python manage.py migrate`, this still save all information in db.sqlite3... someone can help me ? :/
Did you change your django project's settings.py to use postgres instead of the default sqlite?
Thank you.
add apache please
Dang I thought this video was how to connect to a Host Postgres DB and not a dockerized posgres.....
good stuff here thanks for your sup::port !
Thank you too
Thx for work
Would you like to hear a poem?
thanks a lot
You are most welcome
красавчик, чувак!
psycopg2-binary
7:20 You should definitely use psycopg-binary instead of psycopg
psycopg-binary is not recommended for production: www.psycopg.org/docs/install.html#psycopg-vs-psycopg-binary
It's fine for learning and local development though.