If you try to use official Beego’s documentation on Session control you are probably getting errors like:

[E] [server.go:2619] pq: relation "session" does not exist
OR
[server.go:2619] pq: SSL is not enabled on the server
OR
field: Session.SessionData, unsupport field type , may be miss setting tag

here is my quick tip how to fix it.

1) @Postgresql

First of all you need to MANUALLY create table “session” in your Postgres database.

CREATE TABLE session(
    session_key char(64) NOT NULL,
    session_data bytea,
    session_expiry timestamp NOT NULL,
    CONSTRAINT session_key PRIMARY KEY(session_key)
    );

Why manually? Why you can’t just add a tag to Session struct like this:

type Session struct {
    session_key string `orm:"type(char(64))"`
    session_data []byte `orm:"type(bytea)"`
    ...
}

and let ORM to deal with the table.

The problem is in this tag orm:"type(bytea)". Bytes array in MySQL it’s a “blob” type, “bytea” from Postgres won’t work. Obviously author of the Beego is using MySQL and made a mistake here.

2) @app.conf

According to the documentation you need to use SessionSavePath flag to make Postgres work. It’s incorrect. You should replace this with SessionProviderConfig. For example:

SessionProvider = postgresql
SessionProviderConfig = "postgres://username:password@localhost:5432/words?sslmode=disable"
SessionName = session

If you work under Linux you maybe don’t need the ?sslmode=disable flag. And pay attention to SessionProvider = postgresql - you need this ql letters here.

3) @main.go

You have to have this

import (
    ...

    "github.com/astaxie/beego/session"
	_ "github.com/astaxie/beego/session/postgres"
	_ "github.com/lib/pq"
)

4) Finally Beego's session module with Postgres If all is ok, you will have your Beego sessions up and running in Postgres. And a proper cookie on a client.

Search tags: Go, Golang, Beego, Postgresql