sshql -- Go SQL drivers over SSH
This package is forked from github.com/mattn/pqssh package.
package main
import (
"database/sql"
"fmt"
"os"
"github.com/goark/sshql"
"github.com/goark/sshql/pgdrv"
)
func main() {
dialer := &sshql.Dialer{
Hostname: "sshserver",
Port: 22,
Username: "remoteuser",
Password: "passphraseforauthkey",
PrivateKey: "/home/username/.ssh/id_eddsa",
}
pgdrv.New(dialer).Register("postgres+ssh")
db, err := sql.Open("postgres+ssh", "postgres://dbuser:dbpassword@localhost:5432/example?sslmode=disable")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
defer dialer.Close()
defer db.Close()
rows, err := db.Query("SELECT id, name FROM example ORDER BY id")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
for rows.Next() {
var id int64
var name string
if err := rows.Scan(&id, &name); err != nil {
fmt.Fprintln(os.Stderr, err)
break
}
fmt.Printf("ID: %d Name: %s\n", id, name)
}
rows.Close()
}
package main
import (
"database/sql"
"fmt"
"os"
"github.com/goark/sshql"
"github.com/goark/sshql/mysqldrv"
)
func main() {
dialer := &sshql.Dialer{
Hostname: "sshserver",
Port: 22,
Username: "remoteuser",
Password: "passphraseforauthkey",
PrivateKey: "/home/username/.ssh/id_eddsa",
}
mysqldrv.New(dialer).RegisterDial("ssh+tcp")
db, err := sql.Open("mysql", "dbuser:dbpassword@ssh+tcp(localhost:3306)/dbname")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
defer dialer.Close()
defer db.Close()
rows, err := db.Query("SELECT id, name FROM example ORDER BY id")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
for rows.Next() {
var id int64
var name string
if err := rows.Scan(&id, &name); err != nil {
fmt.Fprintln(os.Stderr, err)
break
}
fmt.Printf("ID: %d Name: %s\n", id, name)
}
rows.Close()
}