Skip to content

jiharal/libpostgres

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

libpostgres is a collection of postgresql connections from various sources such as making connections with amp, elastic, etc.

How to use

Connect to database

  dbOptions := libpostgres.Options{
		Host:     "db_host",
		Port:     "db_port",
		Username: "db_username",
		Password: "db_password",
		DBName:   "db_name",
		SSLMode:  "db_sslmode",
	}
	db, err := libpostgres.Connect(dbOptions)
	if err != nil {
		fmt.Println("Error conn to DB", err)
		panic(err)
	}

Migrate database

  • Create directory db/migrations on yout project.
  • Create your file sql with format name
  • 000001_create_users_table.down.sql
  • 000001_create_users_table.up.sql In the .up.sql file let's create the table:
CREATE TABLE IF NOT EXISTS users(
  user_id serial PRIMARY KEY,
  username VARCHAR (50) UNIQUE NOT NULL,
  password VARCHAR (50) NOT NULL,
  email VARCHAR (300) UNIQUE NOT NULL
);

And in the .down.sql let's delete it:

DROP TABLE IF EXISTS users;

Read more here

   err := Migrate(db, "file://./migrations")
   if err != nil {
   	fmt.Println("Error migrate DB", err)
   	panic(err)
   }

Constribute

Please welcome .

About

Lib for postgresql connections

Resources

License

Stars

Watchers

Forks

Packages

No packages published