By creating a PostgreSQL setup script, we can ensure that our database is set up in a specific way to meet our project’s requirements. With the ability to install extensions like the uid extension, we can enhance the functionality of our database. Using a shell script file and specifying the necessary instructions for the PostgreSQL container, we can ensure that our database is created and set up as needed, allowing us to proceed with building our Flask API. This advanced setup option may seem daunting, but it will be beneficial for projects using PostgreSQL database containers. Let’s roll up our sleeves and get ready to build our tables within our application! ππ¦π
Table of Contents
Toggleπ¨βπ» Introduction
This tutorial is part of our YouTube playlist – Try Flask API Development. You can watch this course from the start if you prefer. Alternatively, if you enjoy this course, you can also purchase it on Udemy. Both the playlist and Udemy course links can be found in the video description.
π₯οΈ Working with Docker Containers
Docker containers and images sometimes require the creation of an advanced setup script. This script will create the container’s database in a very specific way to meet the project’s requirements.
π Passing Instructions into the Container
In addition to specifying settings within the docker-compose file, we can also pass instructions into the container. For PostgreSQL database containers, when the container starts, it will look for a setup script and run that script to set up the database specifically for your project needs.
DB Setup Script Creation
Firstly, let’s create a new folder in the root directory of our project called "scripts." Inside the "scripts" folder, let’s create a new file named "db-setup.sh," which is a shell script file containing a series of commands or instructions to be executed by the shell interpreter.
Key Steps in the Script
Shebang Line:
The script specifies the path to the shell interpreter that should be used to execute the script. Since we are using Alpine, we are creating a script that is compatible with the underlying system used in the container.Installing an Extension:
- Postgres is modular, allowing us to install different extensions to enhance or change the functionality of our database. In the script, we install the "uuid" extension to the database we are creating.
Execution of the Script
When initiating our PostgreSQL container, it will run this script to create the database, install the required extension, and set up the table. This will be beneficial for any project utilizing a PostgreSQL database.
Troubleshooting
If you encounter issues with the setup script, ensure that the permissions for the script are set correctly. You can change the file permissions using the command "chmod +x {filename}" to grant execute permission to the script.
π¦ Specifying the Script in the Docker Compose File
Next, we need to specify the setup script within the docker-compose file. We map the "scripts" folder in our project to a folder on the container so that the PostgreSQL database can access and run the setup script.
Creation of Containers
After adding the script, we create the new containers. If you encounter issues when running the script inside the container, troubleshoot by checking the file permissions of the setup script in the container.
π§ Verification of Database Setup
After running the setup, we verify the successful creation of the new database and installation of the "uuid" extension. This ensures that the script worked successfully and the database is set up correctly for our project.
π Conclusion
Assuming that everything has worked as demonstrated in this tutorial, we are now ready to build our Flask API for the project. With the PostgreSQL setup script, we have laid the foundation for a well-configured database within our Docker container.
Related posts:
- How to make a million a month as an IT programmer?
- OpenAI responds to New York Times | “The lawsuit from NYT lacks merit”
- PBLV Season 2, Episode 486 | A new police commissioner at Mistral
- “2024: Easily Install and Utilize Stable Diffusion on Windows (Step-by-Step)
- RAG versus Context Window: Does Gemini 1.5 Pro Really Change the Game? A look at how this new update could be a game-changer.
- Why not follow GEMMA – MARTITA? Keys of the FEMALE circuit with SEBA NERONE | Improve your paddle