title: Unable to connect to postgresql using Go and pq
body: <p>I'm trying to connect a Go application with postgresql.</p>
  <p>The app import postgresql driver:</p>
  <pre><code>&quot;crypto/tls&quot;
  &quot;database/sql&quot;
  &quot;fmt&quot;
  &quot;log&quot;
  &quot;os&quot;
  &quot;os/signal&quot;
   ...
  _ &quot;github.com/go-sql-driver/mysql&quot;
  _ &quot;github.com/lib/pq&quot;
  _ &quot;github.com/mattn/go-sqlite3&quot;
  </code></pre>
  <p>and uses like it to connect to the database:</p>
  <pre><code>driver, cnxn := dbFromURI(dbURI)
  db, err := sql.Open(driver, cnxn)
  if err != nil {
      panic(err)
  }
  </code></pre>
  <p>and the dbFromUri method just split the info</p>
  <pre><code>func dbFromURI(uri string) (string, string) {
      parts := strings.Split(uri, &quot;://&quot;)
      return parts[0], parts[1]
  }
  </code></pre>
  <p>My URI works locally when i run the command : <code>psql postgresql://user:user@172.20.0.1:5432/lcp</code></p>
  <p>But in go i Go, I got</p>
  <pre><code>./lcpserver
  2021/03/07 02:00:42 Reading config /root/lcp-server-install/lcp-home/config/config.yaml
  panic: pq: SSL is not enabled on the server
  </code></pre>
  <p>I tried this URI for Go without success : <code>psql postgresql://user:user@172.20.0.1:5432/lcp?sslmode=disable</code></p>
  <p>Do you have any idea why i can't connect ?
  I tried with my aws rds postgres database, and got same result. Thnaks for the help.</p>
  <p>complete code of the server
  <a href="https://github.com/readium/readium-lcp-server/blob/master/lcpserver/lcpserver.go" rel="nofollow noreferrer">https://github.com/readium/readium-lcp-server/blob/master/lcpserver/lcpserver.go</a></p>
  <p>I change the typo and the demo provided i succeed the connexion. But in the lcp server i style got the same issue.</p>
  <pre><code>postgres://user:user@172.20.0.1:5432/lcp?sslmode=disable
  </code></pre>
  <p>EDIT 2:</p>
  <p>The error is due to the fact that the script tries prepare command. I update the minimal example and it fails too.</p>
  <pre><code>package main
  
  import (
      &quot;database/sql&quot;
      &quot;fmt&quot;
      &quot;strings&quot;
  
      _ &quot;github.com/lib/pq&quot;
  )
  
  func dbFromURI(uri string) (string, string) {
      parts := strings.Split(uri, &quot;://&quot;)
      return parts[0], parts[1]
  }
  
  func main() {
      driver, cnxn := dbFromURI(&quot;postgres://user:user@172.20.0.1:5432/lcp?sslmode=disable&quot;)
  
      fmt.Println(&quot;The driver &quot; + driver)
      fmt.Println(&quot;The cnxn &quot; + cnxn)
  
      db, err := sql.Open(driver, cnxn)
  
       _, err = db.Prepare(&quot;SELECT id,encryption_key,location,length,sha256,type FROM content WHERE id = ? LIMIT 1&quot;)
      if err != nil {
           fmt.Println(&quot;Prepare failed&quot;)
           fmt.Println(err)
  
      }
  
     if err == nil {
          fmt.Println(&quot;Successfully connected&quot;)
      } else {
          panic(err)
      }
  }
  </code></pre>
  <p>I got :
  prepare failed</p>
  <pre><code>pq: SSL is not enabled on the server
  2021/03/07 17:20:13 This panic 3
  panic: pq: SSL is not enabled on the server
  </code></pre>