How to Install and use Postgraphile (Quick Introduction)

By: Arif Khan.

Our Goal

Today I would like to explore the project called PostGraphile. So, I stumbled upon this project some time ago it used to be called PostGraphql. This project will allow you to automatically create GraphQL API, it will create Graphql endpoint from the schema in Postgres database, which is very convenient.

In this blog I would like to give you a short introduction about Postgraphile, hopefully at some point we will use this to create web application.

Why creating Graphql endpoint this way

The idea is you have your database, which the single source of data, and then based on that you create everything, the idea in computer science that you should keep your logic closer to your data which is different than the currently popular code first approach (write your code then manage everything in your code and only do some basic stuff in your database), on the other hand in database first approach you can leverage a lot of interesting features. So, each database has some features which are used in creating web applications e.g. Views, Materialized Views. Some specific vendors like Postgres have even more interesting features. If you are using a client to connect to a database, usually support all those databases, and for that reason, the feature is limited to what’s common across those databases. So, if you are using Postgres which is in my opinion probably the best database, and if you use it directly, you can use those features which have been developed for many years now, then you can create the applications in more efficient way. So, let’s see how you can do that, for this we will do some setup and then we will dive some more deeply.

The first thing you need to create a postgres database while the postgres server is running. I assume that you already have installed the postgres database engine in your computer, if that the case you just type the following command in postgres command prompt to create a database.

createdb graphiletest;

You can check that your database is created or not you can pass below command at prompt to connect newly created db by using psql or pgcli command.

psql graphiletest;


pgcli graphiletest;

You will find that the database is empty. To use postgraphile you have to create schema in database which actually a namespace in postgres. To create schema execute below command.

create schema tstschema;

Now we are ready to create table using this schema. Below command will create table in schema. Note that you have to pass schema name before table name to tell postgres that you want new table in specified schema.

create table tstschema.person(
Id serial primary key,
First_name text,
Last_name text,
Created_at timestamptz not null default now()

Keep this in mind that, while creating tables in postgres use small caps, because if you use capital characters then you have to writ its name always in caps surrounded by double quotes while writing query and it will be very painful. So, don’t use capital characters while create tables.

Now we are good to use postgraphile.

Postgraphile installation.

Node is required to install this package. If you already have Node then just install the package using npm command otherwise you have to install Node first use this link to install Node. Choose your installation package according to your OS.

For postgraphile installation go to command prompt and write below npm command it will install the package in global environment.

npm install postgraphile

It will take a while to finish. Once it finish installation you will be ready to use postgraphile, we also need to  install postgraphile plugin to simplify the graphql endpoints by below command.

npm install @graphile-contrib/pg-simplify-inflector

Using postgraphile

Use following command to start magic.

npx postgraphile -c "postgres://postgres:postgres@localhost:5432/write your databasename here" -s  write your schema name here –watch

If postgraphile start successfully then you will see three start in the end of startup message, like below.

Also watch server will be activated. It will also give you the GraphiQL GUI/IDE link you can use this link to run Graphql playground, which can be seen in below image.

Now you can perform your Graphql queries in this playground.

Keep updated, by reading informative technology articles. You are most welcome to comment.