There is a file in this project called ./requirements.txt which contains the list of Python libraries we will use in this project, such as Pandas, psycopg2, SQLAlchemy, etc. Run the command pip install -r requirements.txt to install all the libraries at once using the PIP (Package Installer for Python).
NOTE: Ensure that the command does not produce any errors in the terminal.
To connect your application to the database, you need to configure a .env file at the root of the project. This file securely stores sensitive credentials and configurations and should be ignored by Git to prevent exposing confidential information. At the root of the project, you’ll find a file called .env.example, which contains a reference format for the database credentials.
Create and open the .env file and add your database credentials.
If you are running the database locally:
DB_HOST="localhost"
DB_PORT= 3306
DB_USER="tu_usuario"
DB_PASSWORD="tu_contraseña"
DB_NAME="nombre_de_tu_base"
If your database is hosted on a cloud service, use the credentials provided by your provider (the values below are examples):
DB_HOST="f565gmi022AD.cbetxkdyhwsb.us-east-1.rds.amazonaws.com"
DB_PORT=3306 # Ajusta el puerto según el servicio
DB_USER="hkietatgd83b4x0l"
DB_PASSWORD="p0s2wasdado1cr02d12"
DB_NAME="y9uflxvx2hsf11g3f"
Make sure you have installed the Postgres client for the terminal called PSQL. You can check if you have it by running the following command:
$ psql --versionNOTE: If you get an error, try following these instructions to install
psqlon your computer.
Now, review the contents of the ./.env file and follow the steps described below:
-
Postgres is a database server, you have to start the server before you can use it. In order to stat postgres in your computer run the following command:
sudo service postgresql start -
Create a new user to connect to your database:
psql -U postgres -c "CREATE USER gitpod;"-
psql -U postgres → Connect to PostgreSQL using the default admin user (postgres).
-
-c "CREATE USER <DB_USER>;" → Execute the SQL command to create a user named <DB_USER>.
If in the .env file you have:
DB_USER = 'my_user'The command you should run is:
`$ psql -U postgres -c "CREATE USER my_user;"`
-
-
Create a new database within the Postgres engine by customizing and executing the following command:
psql -U postgres -c "CREATE DATABASE <DB_NAME> OWNER <DB_USER>;"If in your .env file you have: DB_NAME = 'my_database' DB_USER = 'my_user' Then the command would be: `$ psql -U postgres -c "CREATE DATABASE my_database OWNER my_user;"` -
Connect to the Postgres engine to use your database, manipulate tables and data:
$ psql -h localhost -U <username> <db_name>
NOTE: Remember to check the
./.envfile information to get or set theusernameanddb_name.
Once you are inside PSQL you will be able to create tables, make queries, insert, update, or delete data and much more!
Once you have completed the above steps, you will have created your SQL database, but there are no tables yet. Next, you need to connect to your empty database, and you will create some tables using Python via SQL scripts that you will find in the ./src/sql/ folder:
./src/sql/create.sqlwith all the tables you need to create. Hands on the creation of those tables../src/sql/insert.sqlwith all the table values that will be inserted into each table../src/sql/drop.sqlcontains the SQL code to drop the tables, very useful to clean up your database.
Other important things to mention about the structure:
- Professional projects usually have a
./srcfolder that contains all the coding files (the files that the developer will implement). ./assetsis irrelevant to you; it contains all the images we needed for this tutorial.- The root of any professional project usually contains all the configuration files such as
.gitignore,requirements.txt, etc. You will learn more about these files during the rest of the project.
All your Python code should always be inside the src folder; this is also another good practice.
- The
connectfunction contains the code needed to connect to your Python database. If you look closely, you will see how it loads all the environment variables into a variable calledconnection_stringand then calls thecreate_engineandconnectfunctions.
def connect():
global engine
try:
connection_string = f"postgresql://{os.getenv('DB_USER')}:{os.getenv('DB_PASSWORD')}@{os.getenv('DB_HOST')}/{os.getenv('DB_NAME')}"
print("Starting the connection...")
engine = create_engine(connection_string, isolation_level="AUTOCOMMIT")
engine.connect()
print("Connected successfully!")
return engine
except Exception as e:
print(f"Error connecting to the database: {e}")
return None- We will work on the file inside the src folder called
./src/app.py.
NOTE: This file will contain most of your application code, as well as the database connection and database queries.
-
Create a PostgreSQL database as instructed in the guide.
-
In your
app.py, program the connection to your database.
NOTE: You can try to reuse the
connectPython function provided above.
-
Create the tables indicated in
./src/sql/create.sql. -
Insert the data specified in
./src/sql/insert.sqlinto the tables created in the previous point. -
Use Pandas to print one of the tables as a DataFrame using the
read_sqlfunction of this library.
- Log into your database using
psql -h localhost -u postgres - Connect to a remote database using:
psql -h <REMOTE_HOST> -p <REMOTE_PORT> -U <DB_USER> <DB_NAME> - Deletes a database:
$ dropdb -h localhost -U <username> <db_name> - Create a DB in the render.com cloud and connect following these commands.