示例#1
0
func getdb() *sql.DB {
	var settings, herokupg, localpg string
	herokupg = os.Getenv("DATABASE_URL")
	if herokupg != "" {
		settings, _ = pq.ParseURL(herokupg)
	} else {
		c, _ := goconf.ReadConfigFile("db.config")
		localpg, _ = c.GetString("dev", "postgresurl")
		settings, _ = pq.ParseURL(localpg)
	}
	db, _ := sql.Open("postgres", settings)
	return db
}
示例#2
0
文件: wcld.go 项目: ryandotsmith/wcld
func main() {
	flag.Parse()
	cs, err := pq.ParseURL(os.Getenv("DATABASE_URL"))
	if err != nil {
		log.Println("unable to parse database url")
		os.Exit(1)
	}

	pg, err = sql.Open("postgres", cs)
	if err != nil {
		log.Println("unable to connect to database")
		os.Exit(1)
	}

	log.Println("bind tcp", os.Getenv("PORT"))
	server, err := net.Listen("tcp", "0.0.0.0:"+os.Getenv("PORT"))
	if err != nil {
		log.Println("unable to bind tcp")
		os.Exit(1)
	}
	conns := clientConns(server)
	for {
		go readData(<-conns)
	}
}
示例#3
0
func connectToDB() {
	var err error
	dataSource, err := pq.ParseURL(os.Getenv("PGURL"))
	handleError(err, "URL PARSE")
	DB, err = sql.Open("postgres", dataSource)
	handleError(err, "DB CONNECT")
}
示例#4
0
文件: feel.go 项目: hgmnz/observatory
func feel(databaseUrl string) {
	dataSource, err := pq.ParseURL(databaseUrl)
	if err != nil {
		fmt.Printf("Unable to parse database url (%s)", databaseUrl)
		panic(err)
	}
	u, err := url.Parse(databaseUrl)
	if err != nil {
		panic(err)
	}
	databaseName := u.Path[1:]
	db, err := sql.Open("postgres", dataSource)
	if err != nil {
		fmt.Println("Unable to connect to database")
	}

	for {
		row := db.QueryRow("SELECT count(*) from pg_stat_activity")
		var count uint16
		err = row.Scan(&count)
		if err != nil {
			panic(err)
		}
		row = db.QueryRow("SELECT pg_database_size($1)", databaseName)
		var bytes uint64
		err = row.Scan(&bytes)
		if err != nil {
			panic(err)
		}
		o := observation{connections: count, databaseUrl: databaseUrl, bytes: bytes, serviceAvailable: true, updatedAt: time.Now()}
		o.persist()
	}
}
示例#5
0
文件: web.go 项目: herokai/hk
func initwebdb() {
	connstr, err := pq.ParseURL(os.Getenv("DATABASE_URL"))
	if err != nil {
		log.Fatal("pq.ParseURL", err)
	}
	db, err = sql.Open("postgres", connstr+" sslmode=disable")
	if err != nil {
		log.Fatal("sql.Open", err)
	}
	mustExec(`SET bytea_output = 'hex'`) // work around https://github.com/bmizerany/pq/issues/76
	mustExec(`create table if not exists release (
		plat text not null,
		cmd text not null,
		ver text not null,
		sha256 bytea not null,
		primary key (plat, cmd, ver)
	)`)
	mustExec(`create table if not exists cur (
		plat text not null,
		cmd text not null,
		curver text not null,
		foreign key (plat, cmd, curver) references release (plat, cmd, ver),
		primary key (plat, cmd)
	)`)
	mustExec(`create table if not exists mod (
		t timestamptz not null
	)`)
	mustExec(`insert into mod (t)
		select now()
		where not exists (select 1 from mod)
	`)
}
示例#6
0
func db_open(url string) (*sql.DB, error) {
	conn_str, err := pq.ParseURL(url)
	if err != nil {
		return nil, err
	}
	return sql.Open("postgres", conn_str)
}
示例#7
0
文件: input.go 项目: janne/gometrics
func dbConnect() (db *sql.DB, err error) {
	dbUrl := os.Getenv("DATABASE_URL")
	source, err := pq.ParseURL(dbUrl)
	if err != nil {
		db, err = sql.Open("postgres", source)
	}
	return
}
示例#8
0
func storeBatches(batches <-chan []LogMessage) {
	conn, err := pg.ParseURL(os.Getenv("DATABASE_URL"))

	if err != nil {
		panic(err)
	}

	db, err := sql.Open("postgres", conn)

	if err != nil {
		panic(err)
	}

	_, err = db.Query("SET application_name TO \"log-stuff:storeBatches\";")

	if err != nil {
		panic(err)
	}

	insertStatement, err := db.Prepare("INSERT INTO logs values ($1, $2, $3, $4, $5, $6, $7, $8)")

	if err != nil {
		panic(err)
	}

	rows_inserted := 0

	for batch := range batches {
		tx, err := db.Begin()

		if err != nil {
			panic(err)
		}

		txStmt := tx.Stmt(insertStatement)

		for _, item := range batch {
			_, err := txStmt.Exec(item.facility, item.severity, item.length, item.timestamp, item.drain, item.source, item.process, item.log)
			if err != nil {
				panic(err)
			}
			rows_inserted += 1
		}

		err = tx.Commit()

		if err != nil {
			panic(err)
		}

		rows_inserted = 0
	}
}
示例#9
0
文件: db_test.go 项目: bgentry/gls
func openTestDB(t *testing.T) *sql.DB {
	cs, err := pq.ParseURL("postgres://localhost:5432/gls_test")
	if err != nil {
		t.Fatal(err)
	}
	cs += " sslmode=disable"
	db, err := OpenDB(cs)
	if err != nil {
		t.Fatal(err)
	}
	return db
}
示例#10
0
文件: pg.go 项目: Epictetus/l2met
func init() {
	url := os.Getenv("DATABASE_URL")
	if len(url) == 0 {
		postgresEnabled = false
		fmt.Println("Postgres has been disabled.")
		return
	}
	str, err := pq.ParseURL(url)
	if err != nil {
		fmt.Printf("error=\"unable to parse DATABASE_URL\"\n")
		os.Exit(1)
	}
	pg, err = sql.Open("postgres", str)
	if err != nil {
		fmt.Printf("error=%s\n", err)
		os.Exit(1)
	}

	rurl := os.Getenv("DATABASE_READ_URL")
	if len(rurl) > 0 {
		rstr, err := pq.ParseURL(rurl)
		if err != nil {
			fmt.Printf("error=\"unable to parse DATABASE_READ_URL\"\n")
			os.Exit(1)
		}
		pgRead, err = sql.Open("postgres", rstr)
		if err != nil {
			fmt.Printf("error=%s\n", err)
			os.Exit(1)
		}
		return
	}

	fmt.Printf("Missing DATABASE_READ_URL. Using DATABASE_URL to service reads.\n")
	pgRead, err = sql.Open("postgres", str)
	if err != nil {
		fmt.Printf("error=%s\n", err)
		os.Exit(1)
	}
}
示例#11
0
文件: pg.go 项目: freeformz/l2met
func init() {
	url := os.Getenv("DATABASE_URL")
	if len(url) == 0 {
		fmt.Printf("at=error error=\"must set DATABASE_URL\"\n")
		os.Exit(1)
	}
	str, err := pq.ParseURL(url)
	if err != nil {
		fmt.Printf("at=error error=\"unable to parse DATABASE_URL\"\n")
		os.Exit(1)
	}
	PG, err = sql.Open("postgres", str)
	if err != nil {
		fmt.Printf("at=error error=%s\n", err)
		os.Exit(1)
	}

	rurl := os.Getenv("DATABASE_READ_URL")
	if len(rurl) > 0 {
		rstr, err := pq.ParseURL(rurl)
		if err != nil {
			fmt.Printf("at=error error=\"unable to parse DATABASE_READ_URL\"\n")
			os.Exit(1)
		}
		PGR, err = sql.Open("postgres", rstr)
		if err != nil {
			fmt.Printf("at=error error=%s\n", err)
			os.Exit(1)
		}
		return
	}

	fmt.Printf("Missing DATABASE_READ_URL. Using DATABASE_URL to service reads.\n")
	PGR, err = sql.Open("postgres", str)
	if err != nil {
		fmt.Printf("at=error error=%s\n", err)
		os.Exit(1)
	}
}
示例#12
0
func dbConnect() *sql.DB {
	connStr, err := pq.ParseURL(os.Getenv("DATABASE_URL"))
	if err != nil {
		panic(err)
	}

	db, err := sql.Open("postgres", connStr)
	if err != nil {
		fmt.Printf("Error connecting: %s", err)
	}

	return db
}
func GetConn() *sql.DB {
	// Get the connection string from the DATABASE_URL env var
	connString, err := pq.ParseURL(os.Getenv("DATABASE_URL"))
	if err != nil {
		panic(err)
	}

	db, err := sql.Open("postgres", connString+" sslmode=disable")

	if err != nil {
		panic(err)
	}

	return db
}
示例#14
0
func parseUrl(name string) string {
	var tmp string
	var err error

	tmp = os.Getenv(name)
	if len(tmp) == 0 {
		fmt.Printf("at=error error=\"must set %s\"\n", name)
		os.Exit(1)
	}

	tmp, err = pq.ParseURL(tmp)
	if err != nil {
		fmt.Printf("at=error error=\"unable to parse %s\"\n", name)
		os.Exit(1)
	}
	return tmp
}
示例#15
0
// Get the Postgres Database Url.
func getDatabaseUrl() string {
	var database_url = os.Getenv("DATABASE_URL")
	// Set a default database url if there is nothing in the environemnt
	if database_url == "" {
		// Postgres.app uses this variable to set-up postgres.
		user := os.Getenv("USER")
		// Inject the username into the connection string
		database_url = "postgres://" + user + "@localhost/up?sslmode=disable"
		// Let the user know they are using a default
		fmt.Println("INFO: No DATABASE_URL env var detected, defaulting to " + database_url)
	}
	conn_str, err := pq.ParseURL(database_url)
	if err != nil {
		panic("Unable to Parse DB URL connection string: " + database_url)
	}
	return conn_str
}
示例#16
0
func init() {
	fmt.Println("connecting")
	url := os.Getenv("DATABASE_URL")
	if len(url) == 0 {
		fmt.Printf("error=\"must set DATABASE_URL\"\n")
		os.Exit(1)
	}
	str, err := pq.ParseURL(url)
	if err != nil {
		fmt.Printf("error=\"unable to parse DATABASE_URL\"\n")
		os.Exit(1)
	}
	pg, err = sql.Open("postgres", str)
	if err != nil {
		fmt.Printf("error=%s\n", err)
		os.Exit(1)
	}
}
示例#17
0
文件: pg.go 项目: Epictetus/l2met
func init() {
	url := os.Getenv("DATABASE_URL")
	if len(url) == 0 {
		log.Fatal("Must set DATABASE_URL.")
	}

	pgurl, err := pq.ParseURL(url)
	if err != nil {
		log.Fatal("Unable to parse DATABASE_URL.")
	}

	pg, err = sql.Open("postgres", pgurl)
	if err != nil {
		log.Fatal("Unable to open connection to PostgreSQL.")
	}

	pg.Exec("set application_name = 'l2met-next_postgres'")
}
示例#18
0
func getConnection() *sql.DB {
	config, err := toml.LoadFile("config.toml")
	checkError(err)
	configTree := config.Get("postgres").(*toml.TomlTree)
	user := configTree.Get("user").(string)
	password := configTree.Get("password").(string)
	host := configTree.Get("host").(string)
	database := configTree.Get("database").(string)
	port := configTree.Get("port").(int64)

	rawUrl := fmt.Sprintf(
		"postgres://%s:%s@%s:%d/%s?sslmode=disable",
		user, password, host, port, database,
	)
	url, err := pq.ParseURL(rawUrl)
	checkError(err)
	db, err := sql.Open("postgres", url)
	checkError(err)
	return db
}
示例#19
0
func main() {
	args := os.Args
	if len(args) != 2 {
		conn_example := "postgres://*****:*****@host:port/db"
		fmt.Println("Please provide a connection string in this format",
			conn_example)
		return
	}

	conn, err := pq.ParseURL(args[1])
	if err != nil {
		fmt.Println("Wrong format of connect string.")
		return
	}

	db, openerr := OpenDB(conn)
	if openerr != nil {
		fmt.Println("DB open error", openerr)
		return
	}

	defer db.Close()

	var i int

	row := db.QueryRow("SELECT 1")
	err_ := row.Scan(&i)

	if err_ != nil {
		fmt.Println(err_)
		return
	}

	if i != 1 {
		fmt.Println("'SELECT 1' expects 1, but we got", i)
		return
	}

	fmt.Println("Great, 'SELECT 1' returns", i)
	return
}
示例#20
0
文件: pg.go 项目: ericfode/l2met
func init() {
	url := os.Getenv("DATABASE_URL")
	if len(url) == 0 {
		fmt.Printf("error=\"must set DATABASE_URL\"\n")
		os.Exit(1)
	}

	pgurl, err := pq.ParseURL(url)
	if err != nil {
		fmt.Printf("error=\"unable to parse DATABASE_URL\"\n")
		os.Exit(1)
	}

	pg, err = sql.Open("postgres", pgurl)
	if err != nil {
		fmt.Printf("error=%s\n", err)
		os.Exit(1)
	}

	pg.Exec("set application_name = 'l2met-next_librato'")
}
示例#21
0
文件: open.go 项目: fd/simplex
func Open(url string) (*sql.DB, error) {
	if url == "" {
		url = os.Getenv("DATABASE_URL")
	}

	if url == "" {
		url = os.Getenv("BOXEN_POSTGRESQL_URL")
		if url != "" {
			url += "simplex?sslmode=disable"
		}
	}

	src, err := pq.ParseURL(url)
	if err != nil {
		return nil, err
	}

	db, err := sql.Open("postgres", src)
	if err != nil {
		return nil, err
	}

	return db, nil
}
示例#22
0
文件: db.go 项目: rmacy/hk
func initdb() *sql.DB {
	connstr, err := pq.ParseURL(os.Getenv("DATABASE_URL"))
	if err != nil {
		log.Fatal("pq.ParseURL", err)
	}
	db, err := sql.Open("postgres", connstr+" sslmode=disable")
	if err != nil {
		log.Fatal("sql.Open", err)
	}

	_, err = db.Exec(`
		create table if not exists release (
			plat text,
			cmd text,
			ver text,
			sha1 char(32) not null,
			primary key (plat, cmd, ver)
		)
	`)
	if err != nil {
		log.Fatal(err)
	}

	_, err = db.Exec(`
		create table if not exists cur (
			plat text,
			cmd text,
			curver text not null,
			foreign key (plat, cmd, curver) references release (plat, cmd, ver),
			primary key (plat, cmd)
		)
	`)
	if err != nil {
		log.Fatal(err)
	}

	_, err = db.Exec(`
		create table if not exists patch (
			plat text,
			cmd text,
			oldver text,
			newver text,
			sha1 char(32) not null,
			foreign key (plat, cmd, oldver) references release (plat, cmd, ver),
			foreign key (plat, cmd, newver) references release (plat, cmd, ver),
			primary key (plat, cmd, oldver, newver)
		)
	`)
	if err != nil {
		log.Fatal(err)
	}

	_, err = db.Exec(`
		create table if not exists next (
			plat text,
			cmd text,
			oldver text,
			newver text not null,
			foreign key (plat, cmd, oldver) references release (plat, cmd, ver),
			foreign key (plat, cmd, newver) references release (plat, cmd, ver),
			foreign key (plat, cmd, oldver, newver) references patch (plat, cmd, oldver, newver),
			primary key (plat, cmd, oldver)
		)
	`)
	if err != nil {
		log.Fatal(err)
	}
	return db
}