func DecodeOctal(data []byte) ([]byte, error) {
	output := make([]byte, 0, len(data))
	for i := 0; i < len(data); i++ {
		ch := data[i]
		if !IsPrintableEscapeChar(ch) {
			return nil, ErrDecodeOctalString
		}
		if ch != '\\' {
			output = append(output, ch)
			continue
		}
		if i >= len(data)-1 {
			logrus.Debugln("Encoded string incomplete")
			return nil, ErrDecodeOctalString
		}
		if data[i+1] == '\\' {
			output = append(output, '\\')
			i++
			continue
		}
		if i+3 >= len(data) {
			logrus.Debugln("Encoded string incomplete")
			return nil, ErrDecodeOctalString
		}
		b := byte(0)
		for j := 1; j <= 3; j++ {
			octDigit := data[i+j]
			if octDigit < '0' || octDigit > '7' {
				logrus.Debugln("Invalid bytea escape sequence")
				return nil, ErrDecodeOctalString
			}
			b = (b << 3) | (octDigit - '0')
		}
		output = append(output, b)
		i += 3
	}
	return output, nil
}
DecodeOctal escaped string
See https://www.postgresql.org/docs/current/static/datatype-binary.html#AEN5667
package utils
import (
	"bytes"
	"encoding/hex"
	"errors"
	"fmt"
	"github.com/lib/pq"
	"github.com/sirupsen/logrus"
	"strings"
)
func EncodeToOctal(data []byte) []byte {
	res := make([]byte, 0, len(data))
	for _, c := range []byte(data) {
		if c == '\\' {
			res = append(res, '\\', '\\')
		} else if !IsPrintableEscapeChar(c) {
			res = append(res, '\\', '0'+(c>>6), '0'+((c>>3)&7), '0'+(c&7))
		} else {
			res = append(res, c)
		}
	}
	return res
}
var ErrDecodeOctalString = errors.New("can't decode escaped string")