PostgreSQL

Connecting and working with PostgreSQL and Go.

Links:

Import the driver

  1. Go to https://github.com/lib/pq
  2. Go to the root of the project, and use go get to download the driver:
    $ go get github.com/lib/pq@v1
    
  3. In the Go file that runs the SQL code, import the driver. Because you are not using the driver directly, import it with the blank identifier:
    package main
    
    import "_ github.com/lib/pq"
    

Get a database connection pool

The Postgres data source name (DSN) uses the following format:

postgres://<username>:<password>@<host>/<dbname>

If you set the DSN as an environment variable, you can use it to authenticate to the database:

$ psql $POSTGRES_DSN_ENV_VAR

Viewing databases

PostgreSQL uses meta commands, such as /dt <table-name>.

TODO

pq.Array() method

Array operators and functions

You can add logic to your queries with array functions and operators.

For example, the @> operator checks whether an array contains a value. The following expression returns true if the genres array contains the value that the $2 placeholder represents, or if the placeholder is empty:

(genres @> $2 OR $2 = '{}')