github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/lib/pq/doc.go (about) 1 /* 2 Package pq is a pure Go Postgres driver for the database/sql package. 3 4 In most cases clients will use the database/sql package instead of 5 using this package directly. For example: 6 7 import ( 8 "database/sql" 9 10 _ "github.com/insionng/yougam/libraries/lib/pq" 11 ) 12 13 func main() { 14 db, err := sql.Open("postgres", "user=pqgotest dbname=pqgotest sslmode=verify-full") 15 if err != nil { 16 log.Fatal(err) 17 } 18 19 age := 21 20 rows, err := db.Query("SELECT name FROM users WHERE age = $1", age) 21 … 22 } 23 24 You can also connect to a database using a URL. For example: 25 26 db, err := sql.Open("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full") 27 28 29 Connection String Parameters 30 31 32 Similarly to libpq, when establishing a connection using pq you are expected to 33 supply a connection string containing zero or more parameters. 34 A subset of the connection parameters supported by libpq are also supported by pq. 35 Additionally, pq also lets you specify run-time parameters (such as search_path or work_mem) 36 directly in the connection string. This is different from libpq, which does not allow 37 run-time parameters in the connection string, instead requiring you to supply 38 them in the options parameter. 39 40 For compatibility with libpq, the following special connection parameters are 41 supported: 42 43 * dbname - The name of the database to connect to 44 * user - The user to sign in as 45 * password - The user's password 46 * host - The host to connect to. Values that start with / are for unix domain sockets. (default is localhost) 47 * port - The port to bind to. (default is 5432) 48 * sslmode - Whether or not to use SSL (default is require, this is not the default for libpq) 49 * fallback_application_name - An application_name to fall back to if one isn't provided. 50 * connect_timeout - Maximum wait for connection, in seconds. Zero or not specified means wait indefinitely. 51 * sslcert - Cert file location. The file must contain PEM encoded data. 52 * sslkey - Key file location. The file must contain PEM encoded data. 53 * sslrootcert - The location of the root certificate file. The file must contain PEM encoded data. 54 55 Valid values for sslmode are: 56 57 * disable - No SSL 58 * require - Always SSL (skip verification) 59 * verify-ca - Always SSL (verify that the certificate presented by the server was signed by a trusted CA) 60 * verify-full - Always SSL (verify that the certification presented by the server was signed by a trusted CA and the server host name matches the one in the certificate) 61 62 See http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING 63 for more information about connection string parameters. 64 65 Use single quotes for values that contain whitespace: 66 67 "user=pqgotest password='with spaces'" 68 69 A backslash will escape the next character in values: 70 71 "user=space\ man password='it\'s valid' 72 73 Note that the connection parameter client_encoding (which sets the 74 text encoding for the connection) may be set but must be "UTF8", 75 matching with the same rules as Postgres. It is an error to provide 76 any other value. 77 78 In addition to the parameters listed above, any run-time parameter that can be 79 set at backend start time can be set in the connection string. For more 80 information, see 81 http://www.postgresql.org/docs/current/static/runtime-config.html. 82 83 Most environment variables as specified at http://www.postgresql.org/docs/current/static/libpq-envars.html 84 supported by libpq are also supported by pq. If any of the environment 85 variables not supported by pq are set, pq will panic during connection 86 establishment. Environment variables have a lower precedence than explicitly 87 provided connection parameters. 88 89 The pgpass mechanism as described in http://www.postgresql.org/docs/current/static/libpq-pgpass.html 90 is supported, but on Windows PGPASSFILE must be specified explicitly. 91 92 Queries 93 94 database/sql does not dictate any specific format for parameter 95 markers in query strings, and pq uses the Postgres-native ordinal markers, 96 as shown above. The same marker can be reused for the same parameter: 97 98 rows, err := db.Query(`SELECT name FROM users WHERE favorite_fruit = $1 99 OR age BETWEEN $2 AND $2 + 3`, "orange", 64) 100 101 pq does not support the LastInsertId() method of the Result type in database/sql. 102 To return the identifier of an INSERT (or UPDATE or DELETE), use the Postgres 103 RETURNING clause with a standard Query or QueryRow call: 104 105 var userid int 106 err := db.QueryRow(`INSERT INTO users(name, favorite_fruit, age) 107 VALUES('beatrice', 'starfruit', 93) RETURNING id`).Scan(&userid) 108 109 For more details on RETURNING, see the Postgres documentation: 110 111 http://www.postgresql.org/docs/current/static/sql-insert.html 112 http://www.postgresql.org/docs/current/static/sql-update.html 113 http://www.postgresql.org/docs/current/static/sql-delete.html 114 115 For additional instructions on querying see the documentation for the database/sql package. 116 117 Errors 118 119 pq may return errors of type *pq.Error which can be interrogated for error details: 120 121 if err, ok := err.(*pq.Error); ok { 122 fmt.Println("pq error:", err.Code.Name()) 123 } 124 125 See the pq.Error type for details. 126 127 128 Bulk imports 129 130 You can perform bulk imports by preparing a statement returned by pq.CopyIn (or 131 pq.CopyInSchema) in an explicit transaction (sql.Tx). The returned statement 132 handle can then be repeatedly "executed" to copy data into the target table. 133 After all data has been processed you should call Exec() once with no arguments 134 to flush all buffered data. Any call to Exec() might return an error which 135 should be handled appropriately, but because of the internal buffering an error 136 returned by Exec() might not be related to the data passed in the call that 137 failed. 138 139 CopyIn uses COPY FROM internally. It is not possible to COPY outside of an 140 explicit transaction in pq. 141 142 Usage example: 143 144 txn, err := db.Begin() 145 if err != nil { 146 log.Fatal(err) 147 } 148 149 stmt, err := txn.Prepare(pq.CopyIn("users", "name", "age")) 150 if err != nil { 151 log.Fatal(err) 152 } 153 154 for _, user := range users { 155 _, err = stmt.Exec(user.Name, int64(user.Age)) 156 if err != nil { 157 log.Fatal(err) 158 } 159 } 160 161 _, err = stmt.Exec() 162 if err != nil { 163 log.Fatal(err) 164 } 165 166 err = stmt.Close() 167 if err != nil { 168 log.Fatal(err) 169 } 170 171 err = txn.Commit() 172 if err != nil { 173 log.Fatal(err) 174 } 175 176 177 Notifications 178 179 180 PostgreSQL supports a simple publish/subscribe model over database 181 connections. See http://www.postgresql.org/docs/current/static/sql-notify.html 182 for more information about the general mechanism. 183 184 To start listening for notifications, you first have to open a new connection 185 to the database by calling NewListener. This connection can not be used for 186 anything other than LISTEN / NOTIFY. Calling Listen will open a "notification 187 channel"; once a notification channel is open, a notification generated on that 188 channel will effect a send on the Listener.Notify channel. A notification 189 channel will remain open until Unlisten is called, though connection loss might 190 result in some notifications being lost. To solve this problem, Listener sends 191 a nil pointer over the Notify channel any time the connection is re-established 192 following a connection loss. The application can get information about the 193 state of the underlying connection by setting an event callback in the call to 194 NewListener. 195 196 A single Listener can safely be used from concurrent goroutines, which means 197 that there is often no need to create more than one Listener in your 198 application. However, a Listener is always connected to a single database, so 199 you will need to create a new Listener instance for every database you want to 200 receive notifications in. 201 202 The channel name in both Listen and Unlisten is case sensitive, and can contain 203 any characters legal in an identifier (see 204 http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS 205 for more information). Note that the channel name will be truncated to 63 206 bytes by the PostgreSQL server. 207 208 You can find a complete, working example of Listener usage at 209 http://godoc.org/yougam/libraries/lib/pq/listen_example. 210 211 */ 212 package pq