Summary

In this chapter, we started our journey by introducing PostgreSQL. We saw how to run PostgreSQL in Docker, and then listed out a few basic SQL queries for CRUD operations. We learned how to add new users and roles in PostgreSQL. We then discussed pq, a PostgreSQL driver package for Go, with an example.

We designed a URL-shortening service using the Base62 algorithm. We leveraged pq and gorilla/mux for implementing that service.

PostgreSQL also allows JSON storage (JSONStore) from version 9.2 onward. It allows developers to insert and retrieve JSON documents in a database. It combines the power of both relational and non-relational databases with JSONStore.

We also introduced GORM, a well-known ORM for Go. Using an ORM, database operations can be easily managed. GORM provides a few useful functions—such as AutoMigrate (creating a table if one doesn't exist)—for writing intuitive Go code over the traditional database/sql driver.

Finally, we implemented a REST API for logistics using GORM. PostgreSQL is a well-established, open source relational database that can be a good storage backend for Go. With pq and GORM, driver support for Go is exceptional. The primary goal of this chapter was to make you feel comfortable working with PostgreSQL and Go for REST API development.

So far, we have looked at about building servers that provide REST APIs. Sometimes, developers need client-side tools to consume a REST API. It is also useful to understand how clients can consume a REST API to create a better API. In the next chapter, we will learn generally about how client software is built in Go. There, we also develop API clients for a GitHub REST API.