pq, a pure PostgreSQL database driver for Go
In the previous chapters when we dealt with SQLite3, we used an external library called go-sqlite3
. In the same way, a database driver library is available to bridge both Go and PostgreSQL. That library is called pq
. We can install that library using the command:
go get github.com/lib/pq
After getting this library, we need to use it in a similar way to SQLite3. The API will be in line to the database/sql
package of Go. In order to create a new table, we should initialize the DB. To create a new database, just type this command from the psql
shell, as shown in the following command; it is a one-time thing:
CREATE DATABASE mydb;
Now, we will write a small code illustration that explains the usage of the pq
driver. Create a directory called models
in your $GOPATH
. Here, my GOPATH
is /home/naren/workspace/
. Similar to all the previous examples in the former chapters, we will create our packages and application sources in the src/
directory:
mkdir github...