# Deploy Database For Villain Microservice

## Deploy PostgreSQL database from external container registry

1. Right click in view area then select **Container image** menu.

   ![PostgreSQL database deployment](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-3dbeb68d8219161478e083e1a68e143ccaf41122%2Fdeploy-db-9.png?alt=media)
2. Enter following inputs as a screenshot below:

   * **Image name from external registry:** `registry.redhat.io/rhel9/postgresql-13`
   * **Runtime icon:** `postgresql`
   * **Application:** `Create application`
   * **Application Name:** `villains-service`
   * **Name:** `villains-db`

   ![PostgreSQL database deployment](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-f2a5c1473704d2ee3413a6dbe6bc2659907ed8cc%2Fdeploy-db-10.png?alt=media)
3. Scroll down to to bottom of the page, then **Uncheck "Create a route" checkbox**. And then click on **Deployment** link.

   ![PostgreSQL database deployment](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-23b022fe2dab4ec1df9c02a79f90ec2adfa50203%2Fdeploy-db-12.png?alt=media)
4. Click **Add value** link to add more input fields of environment variables

   ![PostgreSQL database deployment](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-db301139dae6a2b3af10cc917aea5a960972b298%2Fdeploy-db-13.png?alt=media)
5. Enter following environment variables. Then click on **Labels** link.

   * **POSTGRESQL\_USER:** `superbad`
   * **POSTGRESQL\_PASSWORD:** `superbad`
   * **POSTGRESQL\_DATABASE:** `villains_database`

   ![PostgreSQL database deployment](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-4eec695e98cad20dc2071ee7462cff432cdb7fc2%2Fdeploy-db-14.png?alt=media)
6. Enter `system=quarkus-super-heroes` then click **Create** button.

   ![PostgreSQL database deployment](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-5933ca8ab0b1118b4c66cf03446df4304503ea11%2Fdeploy-db-15.png?alt=media)
7. A new `villains-db` PostgreSQL instance should be created. Wait for awhile unti it's up and running.

   ![PostgreSQL database deployment](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-746691e35583d83bf5b43d0b9862cbcc1fbdece2%2Fdeploy-db-16.png?alt=media)

## Initial data to database

1. Click on the **Web Terminal** icon located on top right corner of web console. Wait for a few moment, you should see a terminal shows up.

   ![Web Terminal](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-69329eb8ec9257975070182b2232ec22d5dcaaaa%2Fdeploy-db-17.png?alt=media)
2. Use `curl` command to download SQL script.

   ```
   curl https://raw.githubusercontent.com/rhthsa/developer-advocacy-2022/main/manifest/super-heroes/villains-db-init.sql -o villains-db-init.sql
   ```

   Sample output:

   ```
     % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                Dload  Upload   Total   Spent    Left  Speed
   100  105k  100  105k    0     0   303k      0 --:--:-- --:--:-- --:--:--  303k
   ```
3. Use `psql` (PostgreSQL client) to connect to the `villains-db` PostgreSQL server. **The password is `superbad`**.

   ```
    psql postgresql://villains-db:5432/villains_database?user=superbad
   ```

   Sample output:

   ```
   bash-4.4 ~ $ psql postgresql://villains-db:5432/villains_database?user=superbad
   Password:
   psql (10.21, server 13.7)
   WARNING: psql major version 10, server major version 13.
           Some psql features might not work.
   Type "help" for help.

   villains_database=>
   ```
4. Execute the SQL script with this command:

   ```
    \i villains-db-init.sql
   ```

   Sample output:

   ```
   heroes_database=> \i villains-db-init.sql
   DROP TABLE
   DROP SEQUENCE
   CREATE SEQUENCE
   CREATE TABLE
   INSERT 0 1
   INSERT 0 1
   ....
   ....
   ....
   villains_database=>
   ```
5. Use `\dt` command to check if a new table gets created.

   ```
   \dt
   ```

   Sample output:

   ```
   villains_database=> \dt
         List of relations
   Schema |  Name   | Type  |  Owner
   --------+---------+-------+----------
   public | villain | table | superbad
   (1 row)
   ```
6. Query number of rows in the table.

   ```
   select count(*) from villain;
   ```

   Sample output:

   ```
   villains_database=> select count(*) from villain;
   count
   -------
   100
   (1 row)
   ```
7. Use `\q` command to disconnect from PostgreSQL server.

   ```
   \q
   ```

## What have you learnt?

How to deploy PostgreSQL database service container from external container registry, in this case, Red Hat Registry (can be Docker Hub or other registries as well) with following settings:

* Application icon and name
* Environment variables for application container
* User-defined label


---

# 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://audomsak.gitbook.io/developer-advocacy-2022/application-services-deployment/database-deployment/villain-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.
