Connection refused with Go + Postgres on Heroku - postgresql

I am trying to connect to Heroku's Postgres using a Go. All is working fine locally.
The error I am receiving on Heroku is dial tcp 127.0.0.1:5432: connection refused.
I've confirmed my ability to connect to the database via psql on heroku's command line, and have confirmed that the database url config is correct. The code is clear enough, so I wonder if there is a lower-level problem.
The code is straightforward enough:
import (
"database/sql"
"github.com/coopernurse/gorp"
_ "github.com/lib/pq"
"os"
)
func openDb() *sql.DB {
connection := os.Getenv("DATABASE_URL")
db, err := sql.Open("postgres", connection)
if err != nil {
log.Println(err)
}
return db
}
...and am importing github.com/lib/pq. Go version is 1.1.2.

Heroku + Go is pretty particular about the connection strings. The URL-style doesn't seem to allow specification of sslmode=require, which Heroku insists upon.
The modified version uses pq to parse the URL into a traditional Postgres connection string, and appends the parameter:
import (
"database/sql"
"github.com/lib/pq"
"os"
)
func openDb() *sql.DB {
url := os.Getenv("DATABASE_URL")
connection, _ := pq.ParseURL(url)
connection += " sslmode=require"
db, err := sql.Open("postgres", connection)
if err != nil {
log.Println(err)
}
return db
}

Related

unable to authenticate with mongodb golang driver

I'm using mongodb community version 4.2.13 and go driver version 1.5.
My go application is running on the same host as db, but getting the following error when trying to make a connection:
connection() error occured during connection handshake: auth error:
sasl conversation error: unable to authenticate using mechanism
"SCRAM-SHA-256": (AuthenticationFailed) Authentication failed.
Here is how I created the admin account:
use admin
db.createUser({
user: "admin1",
pwd: "passwd12#$",
roles: ["root"],
mechanisms: ["SCRAM-SHA-256"]
})
db.system.users.update(
{ _id: "admin.admin1", "db": "admin" },
{
$addToSet: {
authenticationRestrictions: { clientSource: ["127.0.0.1"] }
}
}
)
Go app code snippet
package main
import (
"context"
"fmt"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
uri := fmt.Sprintf(
"mongodb://%s:%s#%s:%d/admin?authSource=admin&authMechanism=SCRAM-SHA-256",
"admin1",
"passwd12#$",
"127.0.0.1",
27017,
)
// Prints "mongodb://admin1:passwd12#$#127.0.0.1:27017/admin?authSource=admin&authMechanism=SCRAM-SHA-256"
fmt.Println(uri)
ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Second)
defer cancel()
client, err := mongo.Connect(
ctx,
options.Client().ApplyURI(uri),
)
if err != nil {
panic(err)
}
defer func() {
err = client.Disconnect(ctx)
if err != nil {
panic(err)
}
}()
err = client.Ping(ctx, nil)
if err != nil {
panic(err)
}
fmt.Println("pinged")
}
I tried the following, but all of them didn't work:
Encoding username and password using url.QueryEscape
Trying "localhost" instead of "127.0.0.1"
Removing "authMechanism=SCRAM-SHA-256" in uri
As a side note, connecting to the Mongo shell with the exact same uri, and that worked.
Add ssl=false to your uri. Worked for me
Based on MongoDB documentation for the authentication process, there is a parameter to identify which database is used for authentication besides the target database on the URI.
While in mongoshell you can use this line
mongo "mongodb://Admin:${DBPASSWORD}#<host>:<port>/admin?authSource=admin"
I used that information to add ?authSource=admin to my CONNECTION_URL
CONNECTION_URL=mongodb://root:example#mongo:27017/my_database?retryWrites=true&w=majority&authSource=admin
That worked for me. Hope it does for you too.
For detailed information please review https://www.mongodb.com/features/mongodb-authentication
You could try using 'options.Credential' to pass the authentication settings.
Seems like a cleaner way than formatting an URI that needs to be parsed later on.
https://docs.mongodb.com/drivers/go/current/fundamentals/auth/
clientOpts := options.Client().SetHosts(
[]string{"localhost:27017"},
).SetAuth(
options.Credential{
AuthSource: "<authenticationDb>",
AuthMechanism: "SCRAM-SHA-256",
Username: "<username>",
Password: "<password>",
}
)
client, err := mongo.Connect(context.TODO(), clientOpts)

How to connect PostgreSQL database using Gorm

I am unable to connect to PostgreSQL.
var db *gorm.DB
var err error
func main() {
router := mux.NewRouter()
var err error
db, err = gorm.Open("postgres", "host=localhost port=5432 user=postgres dbname=dvdrental password=12345")
if err != nil {
panic("failed to connect database")
}
I am expecting database to be connected and fetching api.
This is the best way I found to connect to postgres.
import _ "github.com/jinzhu/gorm/dialects/postgres"
dsn := url.URL{
User: url.UserPassword(conf.User, conf.Password),
Scheme: "postgres",
Host: fmt.Sprintf("%s:%d", conf.Host, conf.Port),
Path: conf.DBName,
RawQuery: (&url.Values{"sslmode": []string{"disable"}}).Encode(),
}
db, err := gorm.Open("postgres", dsn.String())
Followed the official docs and it worked for me,
Visit https://gorm.io/docs/connecting_to_the_database.html
import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
dsn := "host=localhost user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
I just had to remove the host variable and enter rest of the details and it works.
First of all, the question keeps spaces for imagination.
Anyway, here're the possible use-cases why you're not able to connect gorm to postgres:
Make sure your postgres is well running in the host you have specified (in this case: localhost)
If it's alright, make sure your user and password is alright
If everything is ok, you can check in the CLI:
$ sudo -i -u postgres
$ psql
If it's successful, that means something else in the code. As far as I can see, you did not show the import section. Did you silently import the postgres gorm dialects?
import _ "github.com/jinzhu/gorm/dialects/postgres"

How to resolve domain names of remote servers while ssh port-forwarding

I am trying to connect to remote mongodb servers using port forwarding. In mgo.DialInfo, the dial server will return a connection (of type *net.Conn) to the intermediary host and the Addrs would be a string of remote mongodb servers' addresses (a set of seed servers). My question is, if I give the remote IP addresses of these servers, the program works fine and I am able to connect to the remote mongodb servers but since, the IP addresses can change I want to give the domain names of the servers, say mongodbserver1 and mongodbserver2 and when I give that, the program hangs. As far as I understood, the program is trying to resolve the domain names on my local machine which won't work and they need to be resolved on the intermediary host. I want to know how I can do that.
In terms of code, the following works fine,
dialinfo := mgo.DialInfo{
Addrs: {"1.2.3.45","1.2.3.56"}
Database: Mongo1,
Username: User,
Password: Pwd,
Timeout: 60 * time.Second,
DialServer: func(addr *mgo.ServerAddr) (net.Conn, error) {
conn, err := connectToCluster("172.12.13.145")
if err != nil {
fmt.Println("couldn't connect to the cluster, trying again..")
return nil, err
}
remote, err := conn.Dial("tcp", addr.String())
if err != nil {
fmt.Println("couldn't connect to the mongodb server:", addr.String())
}
return remote, err
},
}
session, err = mgo.DialWithInfo(dialinfo)
but the one below doesn't work
dialinfo := mgo.DialInfo{
Addrs: {"mongodbserver1","mongodbserver2"}
Database: Mongo1,
Username: User,
Password: Pwd,
Timeout: 60 * time.Second,
DialServer: func(addr *mgo.ServerAddr) (net.Conn, error) {
conn, err := connectToCluster("172.12.13.145")
if err != nil {
fmt.Println("couldn't connect to the cluster, trying again..")
return nil, err
}
remote, err := conn.Dial("tcp", addr.String())
if err != nil {
fmt.Println("couldn't connect to the mongodb server:", addr.String())
}
return remote, err
},
}
session, err = mgo.DialWithInfo(dialinfo)
Configure name resolution to work on your machine. This could mean adding test entries to /etc/hosts (if you're using *nix). Or you could add an entry for your production DNS server to /etc/resolv.conf (again, assuming *nix).

CreateSession: no reachable servers - mgo

I'm trying to connect to MongoDB Atlas free cluster using mgo.
Golang Code -
package main
import (
"fmt"
"gopkg.in/mgo.v2"
"time"
"log"
)
const (
AuthDatabase = "mydatabase"
AuthUserName = "databaseadmin"
AuthPassword = "databasepassword"
ReplicaSetName = "myproject-shard-0"
)
func main(){
MongoDBHosts := []string{
"myproject-shard-00-00-w4vds.mongodb.net:27017",
"myproject-shard-00-01-w4vds.mongodb.net:27017",
"myproject-shard-00-02-w4vds.mongodb.net:27017",
}
mongoDBDialInfo := &mgo.DialInfo{
Addrs: MongoDBHosts,
Timeout: 60 * time.Second,
Database: AuthDatabase,
Username: AuthUserName,
Password: AuthPassword,
ReplicaSetName: ReplicaSetName,
}
mongoSession, err := mgo.DialWithInfo(mongoDBDialInfo)
if err != nil {
log.Fatalf("CreateSession: %s\n", err)
}
defer mongoSession.Close()
fmt.Printf("Connected to replica set %v!\n", mongoSession.LiveServers())
}
Error Message -
CreateSession: no reachable servers
Environment
I'm using mongodb free cluster with Google App Engine GO SDK
To connect to MongoDB Atlas, you need SSL
Prerequisites
TLS/SSL
Clients must have support for TLS/SSL to connect to an Atlas cluster.
Clients must have support for the SNI TLS extension to connect to an Atlas M0 Free Tier cluster.
Whitelist
To access a cluster, you must connect from an IP address on the Atlas group’s IP whitelist. If you need to add an IP address to the whitelist, you can do so in the Connect dialog. You can also add the IP address from the Security tab.
Thus I had to change the following piece of code
mongoDBDialInfo := &mgo.DialInfo{
Addrs: MongoDBHosts,
Timeout: 60 * time.Second,
Database: AuthDatabase,
Username: AuthUserName,
Password: AuthPassword,
ReplicaSetName: ReplicaSetName,
}
mongoDBDialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
return conn, err
}
I had to import the following too
"crypto/tls"
"net"

Can't connect with Mlab database but I can to my local mongo with mgo golang

I am trying to connect to a mlab mongo database in my Golang application but I always get 'auth failed'.
If I use my local mongo, I have no problems (my local doesn't have authentication)
EDIT: I do have created a database user in mLab and I can log in with that user in RoboMongo
My database package looks like this:
package database
import (
"os"
"fmt"
"sync"
"labix.org/v2/mgo"
"time"
)
type DB struct {
Database *mgo.Database
}
const (
MongoDBHosts = "mlabHost:mlabPort"
AuthDatabase = "mydatabase"
AuthUserName = "mlabUser"
AuthPassword = "mlabPassword"
)
var _init_ctx sync.Once
var _instance *DB
func New() *mgo.Database {
_init_ctx.Do(func() {
_instance = new(DB)
mongoDBDialInfo := &mgo.DialInfo{
Addrs: []string{MongoDBHosts},
Timeout: 600 * time.Second,
Database: AuthDatabase,
Username: AuthUserName,
Password: AuthPassword,
}
// Create a session which maintains a pool of socket connections
// to our MongoDB.
session, err := mgo.DialWithInfo(mongoDBDialInfo)
if err != nil {
fmt.Printf("Error en mongo: %+v\n", err)
os.Exit(1)
}
_instance.Database = session.DB(AuthDatabase)
})
return _instance.Database
}
With that code, I always get 'auth failed', but if I change the const values to connect to my local like this:
const (
MongoDBHosts = "localhost:27017"
AuthDatabase = "mydatabase"
AuthUserName = ""
AuthPassword = ""
)
Everything is good.
I can even connect to my mLab database through RoboMongo, but one thing I noticed was that trying to connect from the command line like this:
mongo mLabHost:mLabPort/mydatabase -u mLabUser -p mLabPassword
The prompt asks again for the password and then I get (mypassword = mLabPassword I enter):
2016-06-25T16:07:10.822-0500 E - [main] file [mypassword] doesn't exist
failed to load: mypassword
I tried connecting to mLab in several different ways, but I can't find what is my problem.
Thanks
Found the problem
I was importing the wrong go module.
I had:
labix.org/v2/mgo
instead of:
gopkg.in/mgo.v2
After importing gopkg.in/mgo.v2 it made the connection