Writing an application using Go and PostgreSQL by Henrique Vicente

pgxtutorial

GoDoc Build Status Coverage Status

This is an accompanying repository of the article Back to basics: Writing an application using Go and PostgreSQL by Henrique Vicente. Feel free to open issues to ask any questions or comment on anything.

Environment variables

pgxtutorial uses the following environment variables:

Environment Variable Description
PostgreSQL environment variables Please check https://www.postgresql.org/docs/current/libpq-envars.html
INTEGRATION_TESTDB When running go test, database tests will only run if INTEGRATION_TESTDB=true

tl;dr

To play with it install Go on your system. You'll need to connect to a PostgreSQL database. You can check if a connection is working by calling psql.

To run tests:

# Run all tests passing INTEGRATION_TESTDB explicitly
$ INTEGRATION_TESTDB=true go test -v ./...

To run application:

# Create a database
$ psql -c "CREATE DATABASE pgxtutorial;"
# Set the environment variable PGDATABASE
$ export PGDATABASE=pgxtutorial
# Run migrations
$ tern migrate -m ./migrations
# Execute application
$ go run ./cmd/pgxtutorial
2021/11/22 07:21:21 HTTP server listening at localhost:8080
2021/11/22 07:21:21 gRPC server listening at 127.0.0.1:8082

See also

Owner
Henrique Vicente
Senior Software Engineer
Henrique Vicente
Similar Resources

Using golang framework (Gin) to create a web-application

News feeder Using golang framework (Gin) to create a web-application. This simpl

Aug 22, 2022

Demo application to implement a REST api backend service for an android app using the Go aah framework.

aah-recycleview-backend This tutorial is based on the aah framework to implement a REST API form of CRUD application services, taking "IN-MEMORY" stor

Jan 28, 2022

The source code for workshop Scalable architecture using Redis as backend database using Golang + Redis

The source code for workshop Scalable architecture using Redis as backend database using Golang + Redis

Sep 23, 2022

A simple command line tool using which you can skip phone number based SMS verification by using a temporary phone number that acts like a proxy

A simple command line tool using which you can skip phone number based SMS verification by using a temporary phone number that acts like a proxy

Fake-SMS A simple command line tool using which you can skip phone number based SMS verification by using a temporary phone number that acts like a pr

Dec 31, 2022

Compose sample application Go server with an Nginx proxy and a Postgres database

Compose sample application Go server with an Nginx proxy and a Postgres database Project

Apr 7, 2022

Simple vanilla Go CRUD application with mongoDB database with its mflix dataset that I use for my thesis about benchmarking REST API and GraphQL.

Go GraphQL Simple vanilla Go CRUD application with mongoDB database with its mflix dataset that I use for my thesis about benchmarking REST API and Gr

Oct 15, 2021

This application is a tool to rapidly create TFS tasks and synchronize them with wiki.

This application is a tool to rapidly create TFS tasks and synchronize them with wiki.

Jan 10, 2022

Go application for rooms booking and reservation

Booking Key functionality Showcase properties Allow for booking room for one or more night Book the Room Notify the guest and the owner Have a Back-en

Dec 28, 2021

A real-time serverless chat application with Go and Vue 3

A real-time serverless chat application with Go and Vue 3

sls-chat-app A real-time serverless chat application with Go and Vue 3. For the

Jul 17, 2022
Comments
  • postgres: adding db.WithAcquire() for invoking multiple SQL commands (or *DB functions) reusing the same database connection

    postgres: adding db.WithAcquire() for invoking multiple SQL commands (or *DB functions) reusing the same database connection

    Just some experiments after discussing transactions, connection acquisition, etc. with @Dalot.

    In some circumstances (for example, when seeking performance on a heavy load scenario where the number of available connections is low), we might want to make sure we execute multiple SQL commands using a single connection.

    This quick & dirty change tries to solve this problem providing a function to acquire a connection passed forward via context (*DB.WithAcquire). A Release(ctx) function must be called to return the connection to the pool.

    This feature requires that all *postgres.DB functions use db.conn(ctx) rather than db.Postgres when calling the database to reuse the connection.

    Downsides

    • Introduces some complex behavior in the API (returning database.PGX internally) regarding when a connection is acquired + low risk of acquiring multiple connections without necessity.
    • Easy to end up using db.Postgres (pool) rather than acquired connection (when available) through db.conn(ctx) as it's one more thing you've to be aware of the whole time.

    Alternatives

    Other ways to solve this problem might be:

    • Just duplicate code whenever finding such a scenario where this is desired (at some risk of moving some application code to this database layer).
    • Use a Conn public type required as a second argument to each database function.
Best lightweight, powerful and really fast Api with Golang (Fiber, REL, Dbmate) PostgreSqL

Best lightweight, powerful and really fast Api with Golang (Fiber, REL, Dbmate) PostgreSqL

Dec 26, 2021
REST API written in GO with PostgreSQL and Nginx Proxy + Certbot Let's Encrypt HTTPS certificates + Graphical Frontend in PHP. Deployed via docker-compose.

SSOA-PT REST APP Services Backend: REST API in Go Database: PostgreSQL Web Proxy: Nginx Let's Encrypt HTTPS certificates with certbot Frontend: PHP Ap

Mar 19, 2022
A CRUD Phone Book app by REST-API, Golang and PostgreSQL
A CRUD Phone Book app by REST-API, Golang and PostgreSQL

in this repository, I've created a CRUD Phone Book app by REST-API, Golang and PostgreSQL. in this app, you can: 1-Create a new contact 2-See all contacts 3-Search among the contacts 4-Delete a contact by it's name 5-Delete a contact by it's number 6-Delete all contacts

Oct 8, 2022
This application is used as an example HTTP/SQLite application for Litestream tutorials.

This application is used as an example HTTP/SQLite application for Litestream tutorials. It simply maintains a count of HTTP requests and persists it to a SQLite database.

Apr 2, 2022
A simple database application that I was asked to implement as part of a job application process

This is a simple database application that I was asked to implement as part of a job application process. They told me I could choose any languages an

Nov 24, 2021
A restful api's with Gin Framework with a structured project that defaults to PostgreSQL database
A restful api's with Gin Framework with a structured project that defaults to PostgreSQL database

Welcome to Golang Gin boilerplate v2 The fastest way to deploy a restful api's with Gin Framework with a structured project that defaults to PostgreSQ

Oct 15, 2021
Implementación una base de datos que almacene la información relativa a una tarjeta de crédito utilizando PostgreSQL y BoltDB.

Bases de Datos I: Trabajo Práctico 1. Introducción El objetivo del trabajo práctico es implementar una base de datos que almacene la información relat

Dec 20, 2021
Kaastolon - A fork of solon PostgreSQL HA With our customizations
Kaastolon - A fork of solon PostgreSQL HA With our customizations

kaastolon a fork of stolon - PostgreSQL cloud native High Availability stolon is

Dec 30, 2022
Retro-Floppy UI is a web based application for managing & using a GoTek floppy emulator running the Flash Floppy firmware.
Retro-Floppy UI is a web based application for managing & using a GoTek floppy emulator running the Flash Floppy firmware.

A web user interface for a GoTek running flashfloppy utilising a Raspberry PI 0W as the storage. This allows for remote uploading of files & selecting which disk image is loaded on a retro computer like the BBC Micro or Amiga A1200

Dec 10, 2022
Create a demo RESTful application using Golang

Instructions The goal of this exercise is to create a demo RESTful application using Golang. The Task In this task, we are building backend of an appl

Oct 30, 2021