title: Need to insert struct directly in a PostgreSQL DB
body: <p>I am using go lang as my back end and postgreSQL as my database.I use "github.com/lib/pq" driver to connect with my database. I have a struct which have lot of field and it has some JSON inside it.</p>
  
  <p>My struct looks like this</p>
  
  <pre><code>ApplyLeave1 struct {
          LeaveId           int       
          EmpId             string    
          SupervisorEmpId   string    
          LeaveDays         float64   
          MDays             float64   
          LeaveType         string    
          DayType           string    
          LeaveFrom         time.Time 
          LeaveTo           time.Time 
          AppliedDate       time.Time 
          LeaveStatus       string    
          ResultDate        time.Time     
          Certificate       []*CertificateInfo
      }
  
      CertificateInfo struct {
          Id           int64  
          FileName     string 
          FileType     string 
          FileLocation string 
      } 
  </code></pre>
  
  <p>The struct is big and have some json array inside it and my database schema is as same as the struct. As for as I researched the only way to insert is to use query and insert one by one from mystruct in to the database for example like this</p>
  
  <pre><code>var leave ApplyLeave1
  
      db.Exec("INSERT INTO TABLENAME(leaveid,empid,supervisorid,....) VALUES($1,$2,$3,$4,....)",leave.LeaveId,leave.EmpId,leave.SupervisorId,.....)
  </code></pre>
  
  <p>This is too long to write as my struct is very big.Is it possible to <strong>insert the whole struct at one time</strong> and please direct me <strong>how to insert a json array</strong> inside the database.Appreciate any help.Thanks</p>
  
  <p>Updated information:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS apply_leave1 
  (
      leaveid serial PRIMARY KEY NOT NULL ,
      empid varchar(10) NOT NULL REFERENCES employee(empid),
      leavedays double precision NOT NULL DEFAULT 0 ,
      mdays double precision NOT NULL DEFAULT 0 ,
      leavetype varchar(20) NOT NULL DEFAULT '' ,
      daytype text NOT NULL DEFAULT '',
      leavefrom timestamp with time zone NOT NULL,
      leaveto timestamp with time zone NOT NULL,
      applieddate timestamp with time zone NOT NULL,
      leavestatus varchar(15) NOT NULL DEFAULT ''  ,
      resultdate timestamp with time zone,
      certificatestatus bool NOT NULL DEFAULT FALSE,
      certificate json[])
  </code></pre>
  
  <p>Inside certificate(json[]) i will have all the fields of certificateinfo struct which you can see in the above struct information.Here i give certificate as json[]. I don't know which one is better json or json[] for the retrieval.
  I will be doing following operations and I don't if it is possible with the PostgreSQL.</p>
  
  <ul>
  <li>I need to store more than two certificate information per leave</li>
  <li>I would  like  to give certificate id as the auto increment and unique so it is easy for retrieval( i don`t know if its possible as my leave id is primary key and auto increment as well. </li>
  <li>I will searching the certificate info using leave id and certificate id.</li>
  <li>I need to retrieve only some fields of certificate info.For example only filename and file type using the leave id and certificate id.</li>
  <li>I need to update the certificate information of the particular certificate using its id.</li>
  </ul>
  
  <p>Sorry for being so elaborate.Since i m new to use json in Postgres I have lots of doubts.So please bear my unawareness questions.Thanks...Appreciate your help.Thanks </p>