Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Full-Stack Web Development with Go

You're reading from   Full-Stack Web Development with Go Build your web applications quickly using the Go programming language and Vue.js

Arrow left icon
Product type Paperback
Published in Feb 2023
Publisher Packt
ISBN-13 9781803234199
Length 302 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Nick Glynn Nick Glynn
Author Profile Icon Nick Glynn
Nick Glynn
Nanik Tolaram Nanik Tolaram
Author Profile Icon Nanik Tolaram
Nanik Tolaram
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Part 1: Building a Golang Backend
2. Chapter 1: Building the Database and Model FREE CHAPTER 3. Chapter 2: Application Logging 4. Chapter 3: Application Metrics and Tracing 5. Part 2:Serving Web Content
6. Chapter 4: Serving and Embedding HTML Content 7. Chapter 5: Securing the Backend and Middleware 8. Chapter 6: Moving to API-First 9. Part 3:Single-Page Apps with Vue and Go
10. Chapter 7: Frontend Frameworks 11. Chapter 8: Frontend Libraries 12. Chapter 9: Tailwind, Middleware, and CORS 13. Chapter 10: Session Management 14. Part 4:Release and Deployment
15. Chapter 11: Feature Flags 16. Chapter 12: Building Continuous Integration 17. Chapter 13: Dockerizing an Application 18. Chapter 14: Cloud Deployment 19. Index 20. Other Books You May Enjoy

Setting up Postgres

The database we chose for the sample application is Postgres; we chose Postgres over other databases because of the wide variety of open source tools available for building, configuring, and maintaining Postgres. Postgres has been open source from version 1 since 1989 and it is used by big tech startups worldwide. The project has a lot of community support in terms of tools and utilities, which makes it easier to manage and maintain. The database is suitable for small all the way to big replicated data stores.

The easiest way to run it locally is to run it as a Docker container. First, use the following command to run Postgres:

docker run --name test-postgres \
-e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres

The command will run postgres on port 5432; if by any chance you have other applications or other Postgres instances listening to this port, the command will fail. If you need to run Postgres on a different port, change the -p parameter (for example, -p 5555:5432) to a different port number.

If successful, you will see the container ID printed out. The ID will differ from what is shown here:

f7bdfb7d2c10c5f0c9227c9b0a720f21d3c7fa65907eb0c546b8f20f12621102

Check whether Postgres is up and running by using docker ps. The next thing to do is use the psql-client tool to connect to Postgres to test it out. A list of the different Postgres client tools available on different platforms can be found here: https://wiki.postgresql.org/wiki/PostgreSQL_Clients.

We will use the standard postgres psql tool using Docker. Open another terminal and use the following Docker command to run psql:

docker exec -it test-postgres psql -h localhost -p 5432 -U postgres -d postgres

What we are doing is executing the psql command inside the running Postgres container. You will see output such as the following, indicating that it has successfully connected to the Postgres database:

psql (12.3, server 14.5 (Debian 14.5-1.pgdg110+1))
WARNING: psql major version 12, server major version 14.
         Some psql features might not work.
Type "help" for help.
postgres=#

On a successful connection, you will see the following output. Note that the warning message mentions server major version 14 – this is to indicate that the server version is newer than the current psql version as per the documentation (https://www.postgresql.org/docs/12/app-psql.html). The psql client will work without any problem with the Postgres server:

psql (12.3, server 14.0 (Debian 14.0-1.pgdg110+1))
WARNING: psql major version 12, server major version 14.
         Some psql features might not work.
Type "help" for help.
postgres=#

Exit psql to go back to the command prompt by typing exit.

The following is some guidance on common errors when trying to connect to the database:

Error Message

Description

psql: error: could not connect to server: FATAL: password authentication failed for user “postgres”

The password specified when running Postgres does not match with the password passed in using psql. Check the password.

psql: error: could not connect to server: could not connect to server: Host is unreachable

The IP address that you use to connect to Postgres is wrong.

With this, you have completed the local setup of Postgres and are now ready to start looking into designing the database.

You have been reading a chapter from
Full-Stack Web Development with Go
Published in: Feb 2023
Publisher: Packt
ISBN-13: 9781803234199
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image