This is a quick toy project to explore programmatically creating postgres databases in Flask using a webform.
This simply demonstrates that new postgres databases can be created using a webform in flask.
I have opted to use docker to manage the postgreSQL DBMS. It is a quick and clean way to do this and is a lot less cumbersome than running posgres locally on my machine.
Sure! Here is a sample documentation for your Flask application with PostgreSQL running in a Docker container.
- Docker and Docker Compose installed on your machine.
- Python 3.6 or higher.
- Virtual environment (optional but recommended).
Clone the repository to your local machine:
git clone <repository_url>
cd <repository_directory>
Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate
Install the necessary Python packages:
pip install -r requirements.txt
Ensure your docker-compose.yml
file is configured correctly:
services:
db:
image: postgres:14
container_name: postgres_db
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: Password123
POSTGRES_DB: admin_db
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Start the PostgreSQL container:
docker-compose up -d
Run the Flask application:
python app.py
Open your web browser and navigate to http://localhost:5000/
to access the application.
The landing page displays a welcome message and a button to navigate to the database creation page.
On the database creation page, users can input the name of the new database and create it. The application will check if the database already exists and display appropriate messages.
You can list all databases in the PostgreSQL container using the provided list_databases.sh
script.
-
Create the Script:
Create a file named
list_databases.sh
with the following content:#!/bin/bash CONTAINER_NAME="postgres_db" echo "Listing all databases in the PostgreSQL container '$CONTAINER_NAME':" docker exec -it $CONTAINER_NAME psql -U postgres -c "\l"
-
Make the Script Executable:
chmod +x list_databases.sh
-
Run the Script:
./list_databases.sh
To check if a specific database exists, you can use the following script check_db.sh
:
-
Create the Script:
Create a file named
check_db.sh
with the following content:#!/bin/bash DB_NAME="$1" CONTAINER_NAME="postgres_db" EXISTS=$(docker exec -it $CONTAINER_NAME psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'") if [ "$EXISTS" = "1" ]; then echo "Database '$DB_NAME' exists." else echo "Database '$DB_NAME' does not exist." fi
-
Make the Script Executable:
chmod +x check_db.sh
-
Run the Script:
./check_db.sh <database_name>
app.py
: Main application file.forms.py
: Contains the form for database creation.templates/
: Contains HTML templates.index.html
: Landing page template.create_db.html
: Database creation page template.
list_databases.sh
: Script to list all databases.check_db.sh
: Script to check the existence of a specific database.
The Flask application uses environment variables for configuration. Update these variables in your docker-compose.yml
file:
POSTGRES_USER
: PostgreSQL user.POSTGRES_PASSWORD
: PostgreSQL password.POSTGRES_DB
: Default database.
-
Database Role Does Not Exist:
If you encounter an error indicating that the role does not exist, ensure that the role is created within the PostgreSQL container.
docker exec -it postgres_db psql -U postgres -c "CREATE ROLE your_username WITH LOGIN PASSWORD 'your_password';"
-
Connection Issues:
Ensure the PostgreSQL container is running and accessible at the specified ports.
docker-compose ps
To view logs for the PostgreSQL container:
docker logs postgres_db