Using PostgreSQL with ngrok
Test your API locally using ngrok and a PostgreSQL database.
Before you begin
Install ngrok on your machine, using the appropriate tab for your operating
system.
About this task
The following steps are for using PostgreSQL on Mac. If you are using Windows, Chocolatey follows a similar installation method for PostgreSQL.
Procedure
-
Install PostgreSQL.
If you already have a PostgreSQL database running locally, skip to the next step.
- (Optional) Install pgAdmin if you want to see your database outside of the CLI.
- Install PostgreSQL using Homebrew:
brew install postgresql - Start the PostgreSQL server:
brew services start postgresql - Install
psqlso you can run PostgreSQL commands:brew link --force libpq - Verify that PostgreSQL is installed:
psql --version psql (PostgreSQL) 13.3 - Access the PostgreSQL server:
psql postgres - Create a table in the new database by pasting the following content on the command
line:
postgres-# CREATE TABLE weather ( city varchar(80), temp_lo int, -- low temperature temp_hi int, -- high temperature prcp real, -- precipitation date date );
- Point
ngrokto your local PostgreSQL server.- Run the following command to determine the port that the PostgreSQL server is
listening on:
postgres=# \conninfo You are connected to database "postgres" as user "testuser" via socket in "/tmp" at port "5432".In the example, the port is
5432. - Run the following command, changing the port to the value that you determine in the
previous step:
ngrok tcp 5432 // change 5432 to your port numberThe command generates the TCP endpoint that you will use later in the
config.yamlfile:Session Status online Account Test Account Version 2.3.40 Region United States (us) Web Interface http://127.0.0.1:1234 Forwarding tcp://2.tcp.ngrok.io:1234 -> localhost:5432 Connections ttl opn rt1 rt5 p50 p90 0 0 0.00 0.00 0.00 0.00
- Run the following command to determine the port that the PostgreSQL server is
listening on:
- Connect PostgreSQL to API Connect for GraphQL as a Service.
From the information collected earlier with the
\conninfoandngrok tcp 5432commands, add the PostgreSQL TCP endpoint, along with the username and password from the database, toconfig.yamlfor the schema.- Create the
config.yamlconfiguration file with the following content:
where:configurationset: - configuration: name: pg_config uri: postgresql://username:password@2.tcp.ngrok.io:1234/postgresnameis a name for this configuration. If you are adding this configuration to an existing GraphQL schema, change the name of the configuration to match the name in your schema.uricontains your database user name and password, the TCP endpoint that was created when you ran thengrok tcp 5432command in the previous step, and the database name using the following format:uri: postgresql://<db_user>:<db_password>@<tcp_endpoint>/<db_name>In the sample configuration file:uri: postgresql://your_username:your_password@2.tcp.ngrok.io:1234/postgres<db_user>isyour_username<db_password>isyour_assword<tcp_endpoint>is2.tcp.ngrok.io:1234<db_name>ispostgres
- List the tables in the database by running the following command:
postgres=# \c postgres You are now connected to database "postgres" as user "your_username". postgres=# \dt List of relations Schema | Name | Type | Owner --------+---------+-------+------------ public | weather | table | root (1 row) - Create the following GraphQL schema to execute one mutation and one query to the
tutorialdbdatabase:- The
createWeathermutation creates new rows in theweathertable. - The
weatherquery retrieves all of the existing rows in theweathertable.
type Weather { city: String temp_lo: Int temp_hi: Int prcp: Float date: Date } type Query { weather: [Weather] @dbquery( type: "postgresql" query: "select * from weather" configuration: "pg_config" ) } type Mutation { createWeather(city: String!, temp_lo: Int!, temp_hi: Int!, prcp: Float!, date: Date!): Weather @dbquery( type: "postgresql" table: "weather" dml: INSERT configuration: "pg_config" ) } - The
- Create the