# Setup App Database & connect to database

In our example we have the posts that will have to be added to a table in our Database

* The <mark style="color:purple;">**posts**</mark> table will have to have the following columns:

![](/files/juKi7OX1fgdvuo4RM0to)

* Also added 2 mock posts to the database

![](/files/g7pK9DCMQRKwLi1UOv2n)

#### To connect Python to our Database

* We will need a Postgres Driver
* There are multiple libraries that are able to do that but we ill use the [Psycopg Library](https://www.psycopg.org/)
* To set this up for Psycopg3 use [this documentation](https://www.psycopg.org/psycopg3/docs/)
* [Basic module usage](https://www.psycopg.org/psycopg3/docs/basic/usage.html) will help you set up the Database connection

#### To connect to the database we need to set up the following code

```python
import psycopg

try:
    conn = psycopg.connect("dbname=fastapi user=postgres password=Password1")
    cursor = conn.cursor()
    print("Database connection successful")

except Exception as error:
    print("Connecting to database failed")
    print("Error: ", error)
```

* This should either connect or return the error that was raised

{% hint style="info" %}
If there is any python code that can fail, it is best to use the try/except block in order to not break the code
{% endhint %}

* The main problem with the above code is that if the database is not reachable, there is no point in having our server up
* One solution would be to add the whole thing into a <mark style="color:orange;">`while`</mark> loop

```python
while True:

    try:
        conn = psycopg.connect("dbname=fastapi user=postgres password=Password1")
        cursor = conn.cursor()
        print("Database connection successful")
        break

    except Exception as error:
        print("Connecting to database failed")
        print("Error: ", error)
        time.sleep(2)
```

* We will break out of the loop if the connection is successful
* If the connection does not work, it will wait 2 seconds and restart the loop

{% hint style="danger" %}
**Hint**: We have hardcoded our database credentials in our code

This is really bad practice, but as we are learning, this is fine for now.&#x20;
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.arkannis.net/programming/python/frameworks/fastapi/setup-app-database-and-connect-to-database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
