Creating a setup script for PostgreSQL – Exploring Flask API Development

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! πŸ˜πŸ“¦πŸš€

πŸ‘¨β€πŸ’» 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

  1. 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.

  2. 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.

About the Author

Very Academy
61.2K subscribers

About the Channel:

Very Academy is an online education provider that offers a vast and ever-growing catalogue of tutorials and courses. Very Academy’s mission is to provide universally accessible, highest quality Computer Science and Technology education.By providing you with the most relevant, highest-quality learning experiences to support your learning, we hope to help foster students into globally competent professionals in the field of Computer Science and Technology.
Share the Post:
en_GBEN_GB