1647981840
pgx - PostgreSQL Driver and Toolkit
pgx is a pure Go driver and toolkit for PostgreSQL.
pgx aims to be low-level, fast, and performant, while also enabling PostgreSQL-specific features that the standard database/sql
package does not allow for.
The driver component of pgx can be used alongside the standard database/sql
package.
The toolkit component is a related set of packages that implement PostgreSQL functionality such as parsing the wire protocol and type mapping between PostgreSQL and Go. These underlying packages can be used to implement alternative drivers, proxies, load balancers, logical replication clients, etc.
The current release of pgx v4
requires Go modules. To use the previous version, checkout and vendor the v3
branch.
package main
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v4"
)
func main() {
// urlExample := "postgres://username:password@localhost:5432/database_name"
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close(context.Background())
var name string
var weight int64
err = conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
fmt.Println(name, weight)
}
See the getting started guide for more information.
It is recommended to use the pgx interface if:
database/sql
are in use.The pgx interface is faster and exposes more features.
The database/sql
interface only allows the underlying driver to return or receive the following types: int64
, float64
, bool
, []byte
, string
, time.Time
, or nil
. Handling other types requires implementing the database/sql.Scanner
and the database/sql/driver/driver.Valuer
interfaces which require transmission of values in text format. The binary format can be substantially faster, which is what the pgx interface uses.
pgx supports many features beyond what is available through database/sql
:
log15adapter
, logrus
, zap
, and zerolog
inet
and cidr
PostgreSQL types to net.IPNet
and net.IP
database/sql.Scanner
and database/sql/driver.Valuer
interfaces for custom typesThere are three areas in particular where pgx can provide a significant performance advantage over the standard database/sql
interface and other drivers:
pgx tests naturally require a PostgreSQL database. It will connect to the database specified in the PGX_TEST_DATABASE
environment variable. The PGX_TEST_DATABASE
environment variable can either be a URL or DSN. In addition, the standard PG*
environment variables will be respected. Consider using direnv to simplify environment variable handling.
Connect to your PostgreSQL server and run:
create database pgx_test;
Connect to the newly-created database and run:
create domain uint64 as numeric(20,0);
Now, you can run the tests:
PGX_TEST_DATABASE="host=/var/run/postgresql database=pgx_test" go test ./...
In addition, there are tests specific for PgBouncer that will be executed if PGX_TEST_PGBOUNCER_CONN_STRING
is set.
pgx supports the same versions of Go and PostgreSQL that are supported by their respective teams. For Go that is the two most recent major releases and for PostgreSQL the major releases in the last 5 years. This means pgx supports Go 1.16 and higher and PostgreSQL 10 and higher. pgx also is tested against the latest version of CockroachDB.
pgx follows semantic versioning for the documented public API on stable releases. v4
is the latest stable major version.
pgx is the head of a family of PostgreSQL libraries. Many of these can be used independently. Many can also be accessed from pgx for lower-level control.
pgconn
is a lower-level PostgreSQL database driver that operates at nearly the same level as the C library libpq
.
pgxpool
is a connection pool for pgx. pgx is entirely decoupled from its default pool implementation. This means that pgx can be used with a different pool or without any pool at all.
This is a database/sql
compatibility layer for pgx. pgx can be used as a normal database/sql
driver, but at any time, the native interface can be acquired for more performance or PostgreSQL specific functionality.
Over 70 PostgreSQL types are supported including uuid
, hstore
, json
, bytea
, numeric
, interval
, inet
, and arrays. These types support database/sql
interfaces and are usable outside of pgx. They are fully tested in pgx and pq. They also support a higher performance interface when used with the pgx driver.
pgproto3 provides standalone encoding and decoding of the PostgreSQL v3 wire protocol. This is useful for implementing very low level PostgreSQL tooling.
pglogrepl provides functionality to act as a client for PostgreSQL logical replication.
pgmock offers the ability to create a server that mocks the PostgreSQL wire protocol. This is used internally to test pgx by purposely inducing unusual errors. pgproto3 and pgmock together provide most of the foundational tooling required to implement a PostgreSQL proxy or MitM (such as for a custom connection pooler).
tern is a stand-alone SQL migration system.
pgerrcode contains constants for the PostgreSQL error codes.
Library for scanning data from a database into Go structs and more.
Author: Jackc
Source Code: https://github.com/jackc/pgx
License: MIT License
1599854400
Go announced Go 1.15 version on 11 Aug 2020. Highlighted updates and features include Substantial improvements to the Go linker, Improved allocation for small objects at high core counts, X.509 CommonName deprecation, GOPROXY supports skipping proxies that return errors, New embedded tzdata package, Several Core Library improvements and more.
As Go promise for maintaining backward compatibility. After upgrading to the latest Go 1.15 version, almost all existing Golang applications or programs continue to compile and run as older Golang version.
#go #golang #go 1.15 #go features #go improvement #go package #go new features
1648926060
pgx is a pure Go driver and toolkit for PostgreSQL.
pgx aims to be low-level, fast, and performant, while also enabling PostgreSQL-specific features that the standard database/sql
package does not allow for.
The driver component of pgx can be used alongside the standard database/sql
package.
The toolkit component is a related set of packages that implement PostgreSQL functionality such as parsing the wire protocol and type mapping between PostgreSQL and Go. These underlying packages can be used to implement alternative drivers, proxies, load balancers, logical replication clients, etc.
The current release of pgx v4
requires Go modules. To use the previous version, checkout and vendor the v3
branch.
package main
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v4"
)
func main() {
// urlExample := "postgres://username:password@localhost:5432/database_name"
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close(context.Background())
var name string
var weight int64
err = conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
fmt.Println(name, weight)
}
See the getting started guide for more information.
It is recommended to use the pgx interface if:
database/sql
are in use.The pgx interface is faster and exposes more features.
The database/sql
interface only allows the underlying driver to return or receive the following types: int64
, float64
, bool
, []byte
, string
, time.Time
, or nil
. Handling other types requires implementing the database/sql.Scanner
and the database/sql/driver/driver.Valuer
interfaces which require transmission of values in text format. The binary format can be substantially faster, which is what the pgx interface uses.
pgx supports many features beyond what is available through database/sql
:
log15adapter
, logrus
, zap
, and zerolog
inet
and cidr
PostgreSQL types to net.IPNet
and net.IP
database/sql.Scanner
and database/sql/driver.Valuer
interfaces for custom typesThere are three areas in particular where pgx can provide a significant performance advantage over the standard database/sql
interface and other drivers:
pgx tests naturally require a PostgreSQL database. It will connect to the database specified in the PGX_TEST_DATABASE
environment variable. The PGX_TEST_DATABASE
environment variable can either be a URL or DSN. In addition, the standard PG*
environment variables will be respected. Consider using direnv to simplify environment variable handling.
Connect to your PostgreSQL server and run:
create database pgx_test;
Connect to the newly-created database and run:
create domain uint64 as numeric(20,0);
Now, you can run the tests:
PGX_TEST_DATABASE="host=/var/run/postgresql database=pgx_test" go test ./...
In addition, there are tests specific for PgBouncer that will be executed if PGX_TEST_PGBOUNCER_CONN_STRING
is set.
pgx supports the same versions of Go and PostgreSQL that are supported by their respective teams. For Go that is the two most recent major releases and for PostgreSQL the major releases in the last 5 years. This means pgx supports Go 1.16 and higher and PostgreSQL 10 and higher. pgx also is tested against the latest version of CockroachDB.
pgx follows semantic versioning for the documented public API on stable releases. v4
is the latest stable major version.
pgx is the head of a family of PostgreSQL libraries. Many of these can be used independently. Many can also be accessed from pgx for lower-level control.
pgconn
is a lower-level PostgreSQL database driver that operates at nearly the same level as the C library libpq
.
pgxpool
is a connection pool for pgx. pgx is entirely decoupled from its default pool implementation. This means that pgx can be used with a different pool or without any pool at all.
This is a database/sql
compatibility layer for pgx. pgx can be used as a normal database/sql
driver, but at any time, the native interface can be acquired for more performance or PostgreSQL specific functionality.
Over 70 PostgreSQL types are supported including uuid
, hstore
, json
, bytea
, numeric
, interval
, inet
, and arrays. These types support database/sql
interfaces and are usable outside of pgx. They are fully tested in pgx and pq. They also support a higher performance interface when used with the pgx driver.
pgproto3 provides standalone encoding and decoding of the PostgreSQL v3 wire protocol. This is useful for implementing very low level PostgreSQL tooling.
pglogrepl provides functionality to act as a client for PostgreSQL logical replication.
pgmock offers the ability to create a server that mocks the PostgreSQL wire protocol. This is used internally to test pgx by purposely inducing unusual errors. pgproto3 and pgmock together provide most of the foundational tooling required to implement a PostgreSQL proxy or MitM (such as for a custom connection pooler).
tern is a stand-alone SQL migration system.
pgerrcode contains constants for the PostgreSQL error codes.
Library for scanning data from a database into Go structs and more.
Author: jackc
Source Code: https://github.com/jackc/pgx
License: MIT License
1660299780
The MongoDB supported driver for Go.
The recommended way to get started using the MongoDB Go driver is by using Go modules to install the dependency in your project. This can be done either by importing packages from go.mongodb.org/mongo-driver
and having the build step install the dependency or by explicitly running
go get go.mongodb.org/mongo-driver/mongo
When using a version of Go that does not support modules, the driver can be installed using dep
by running
dep ensure -add "go.mongodb.org/mongo-driver/mongo"
To get started with the driver, import the mongo
package and create a mongo.Client
with the Connect
function:
import (
"context"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
Make sure to defer a call to Disconnect
after instantiating your client:
defer func() {
if err = client.Disconnect(ctx); err != nil {
panic(err)
}
}()
For more advanced configuration and authentication, see the documentation for mongo.Connect.
Calling Connect
does not block for server discovery. If you wish to know if a MongoDB server has been found and connected to, use the Ping
method:
ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
err = client.Ping(ctx, readpref.Primary())
To insert a document into a collection, first retrieve a Database
and then Collection
instance from the Client
:
collection := client.Database("testing").Collection("numbers")
The Collection
instance can then be used to insert documents:
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
res, err := collection.InsertOne(ctx, bson.D{{"name", "pi"}, {"value", 3.14159}})
id := res.InsertedID
To use bson.D
, you will need to add "go.mongodb.org/mongo-driver/bson"
to your imports.
Your import statement should now look like this:
import (
"context"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
Several query methods return a cursor, which can be used like this:
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cur, err := collection.Find(ctx, bson.D{})
if err != nil { log.Fatal(err) }
defer cur.Close(ctx)
for cur.Next(ctx) {
var result bson.D
err := cur.Decode(&result)
if err != nil { log.Fatal(err) }
// do something with result....
}
if err := cur.Err(); err != nil {
log.Fatal(err)
}
For methods that return a single item, a SingleResult
instance is returned:
var result struct {
Value float64
}
filter := bson.D{{"name", "pi"}}
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err = collection.FindOne(ctx, filter).Decode(&result)
if err == mongo.ErrNoDocuments {
// Do something when no record was found
fmt.Println("record does not exist")
} else if err != nil {
log.Fatal(err)
}
// Do something with result...
Additional examples and documentation can be found under the examples directory and on the MongoDB Documentation website.
For help with the driver, please post in the MongoDB Community Forums.
New features and bugs can be reported on jira: https://jira.mongodb.org/browse/GODRIVER
The driver tests can be run against several database configurations. The most simple configuration is a standalone mongod with no auth, no ssl, and no compression. To run these basic driver tests, make sure a standalone MongoDB server instance is running at localhost:27017. To run the tests, you can run make
(on Windows, run nmake
). This will run coverage, run go-lint, run go-vet, and build the examples.
To test a replica set or sharded cluster, set MONGODB_URI="<connection-string>"
for the make
command. For example, for a local replica set named rs1
comprised of three nodes on ports 27017, 27018, and 27019:
MONGODB_URI="mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs1" make
To test authentication and TLS, first set up a MongoDB cluster with auth and TLS configured. Testing authentication requires a user with the root
role on the admin
database. Here is an example command that would run a mongod with TLS correctly configured for tests. Either set or replace PATH_TO_SERVER_KEY_FILE and PATH_TO_CA_FILE with paths to their respective files:
mongod \
--auth \
--tlsMode requireTLS \
--tlsCertificateKeyFile $PATH_TO_SERVER_KEY_FILE \
--tlsCAFile $PATH_TO_CA_FILE \
--tlsAllowInvalidCertificates
To run the tests with make
, set:
MONGO_GO_DRIVER_CA_FILE
to the location of the CA file used by the databaseMONGO_GO_DRIVER_KEY_FILE
to the location of the client key fileMONGO_GO_DRIVER_PKCS8_ENCRYPTED_KEY_FILE
to the location of the pkcs8 client key file encrypted with the password string: password
MONGO_GO_DRIVER_PKCS8_UNENCRYPTED_KEY_FILE
to the location of the unencrypted pkcs8 key fileMONGODB_URI
to the connection string of the serverAUTH=auth
SSL=ssl
For example:
AUTH=auth SSL=ssl \
MONGO_GO_DRIVER_CA_FILE=$PATH_TO_CA_FILE \
MONGO_GO_DRIVER_KEY_FILE=$PATH_TO_CLIENT_KEY_FILE \
MONGO_GO_DRIVER_PKCS8_ENCRYPTED_KEY_FILE=$PATH_TO_ENCRYPTED_KEY_FILE \
MONGO_GO_DRIVER_PKCS8_UNENCRYPTED_KEY_FILE=$PATH_TO_UNENCRYPTED_KEY_FILE \
MONGODB_URI="mongodb://user:password@localhost:27017/?authSource=admin" \
make
Notes:
--tlsAllowInvalidCertificates
flag is required on the server for the test suite to work correctly.?authSource=admin
, not /admin
.The MongoDB Go Driver supports wire protocol compression using Snappy, zLib, or zstd. To run tests with wire protocol compression, set MONGO_GO_DRIVER_COMPRESSOR
to snappy
, zlib
, or zstd
. For example:
MONGO_GO_DRIVER_COMPRESSOR=snappy make
Ensure the --networkMessageCompressors
flag on mongod or mongos includes zlib
if testing zLib compression.
Check out the project page for tickets that need completing. See our contribution guidelines for details.
Commits to master are run automatically on evergreen.
See our common issues documentation for troubleshooting frequently encountered issues.
@ashleymcnamara - Mongo Gopher Artwork
Author: Mongodb
Source Code: https://github.com/mongodb/mongo-go-driver
License: Apache-2.0 license
1647981840
pgx - PostgreSQL Driver and Toolkit
pgx is a pure Go driver and toolkit for PostgreSQL.
pgx aims to be low-level, fast, and performant, while also enabling PostgreSQL-specific features that the standard database/sql
package does not allow for.
The driver component of pgx can be used alongside the standard database/sql
package.
The toolkit component is a related set of packages that implement PostgreSQL functionality such as parsing the wire protocol and type mapping between PostgreSQL and Go. These underlying packages can be used to implement alternative drivers, proxies, load balancers, logical replication clients, etc.
The current release of pgx v4
requires Go modules. To use the previous version, checkout and vendor the v3
branch.
package main
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v4"
)
func main() {
// urlExample := "postgres://username:password@localhost:5432/database_name"
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close(context.Background())
var name string
var weight int64
err = conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
fmt.Println(name, weight)
}
See the getting started guide for more information.
It is recommended to use the pgx interface if:
database/sql
are in use.The pgx interface is faster and exposes more features.
The database/sql
interface only allows the underlying driver to return or receive the following types: int64
, float64
, bool
, []byte
, string
, time.Time
, or nil
. Handling other types requires implementing the database/sql.Scanner
and the database/sql/driver/driver.Valuer
interfaces which require transmission of values in text format. The binary format can be substantially faster, which is what the pgx interface uses.
pgx supports many features beyond what is available through database/sql
:
log15adapter
, logrus
, zap
, and zerolog
inet
and cidr
PostgreSQL types to net.IPNet
and net.IP
database/sql.Scanner
and database/sql/driver.Valuer
interfaces for custom typesThere are three areas in particular where pgx can provide a significant performance advantage over the standard database/sql
interface and other drivers:
pgx tests naturally require a PostgreSQL database. It will connect to the database specified in the PGX_TEST_DATABASE
environment variable. The PGX_TEST_DATABASE
environment variable can either be a URL or DSN. In addition, the standard PG*
environment variables will be respected. Consider using direnv to simplify environment variable handling.
Connect to your PostgreSQL server and run:
create database pgx_test;
Connect to the newly-created database and run:
create domain uint64 as numeric(20,0);
Now, you can run the tests:
PGX_TEST_DATABASE="host=/var/run/postgresql database=pgx_test" go test ./...
In addition, there are tests specific for PgBouncer that will be executed if PGX_TEST_PGBOUNCER_CONN_STRING
is set.
pgx supports the same versions of Go and PostgreSQL that are supported by their respective teams. For Go that is the two most recent major releases and for PostgreSQL the major releases in the last 5 years. This means pgx supports Go 1.16 and higher and PostgreSQL 10 and higher. pgx also is tested against the latest version of CockroachDB.
pgx follows semantic versioning for the documented public API on stable releases. v4
is the latest stable major version.
pgx is the head of a family of PostgreSQL libraries. Many of these can be used independently. Many can also be accessed from pgx for lower-level control.
pgconn
is a lower-level PostgreSQL database driver that operates at nearly the same level as the C library libpq
.
pgxpool
is a connection pool for pgx. pgx is entirely decoupled from its default pool implementation. This means that pgx can be used with a different pool or without any pool at all.
This is a database/sql
compatibility layer for pgx. pgx can be used as a normal database/sql
driver, but at any time, the native interface can be acquired for more performance or PostgreSQL specific functionality.
Over 70 PostgreSQL types are supported including uuid
, hstore
, json
, bytea
, numeric
, interval
, inet
, and arrays. These types support database/sql
interfaces and are usable outside of pgx. They are fully tested in pgx and pq. They also support a higher performance interface when used with the pgx driver.
pgproto3 provides standalone encoding and decoding of the PostgreSQL v3 wire protocol. This is useful for implementing very low level PostgreSQL tooling.
pglogrepl provides functionality to act as a client for PostgreSQL logical replication.
pgmock offers the ability to create a server that mocks the PostgreSQL wire protocol. This is used internally to test pgx by purposely inducing unusual errors. pgproto3 and pgmock together provide most of the foundational tooling required to implement a PostgreSQL proxy or MitM (such as for a custom connection pooler).
tern is a stand-alone SQL migration system.
pgerrcode contains constants for the PostgreSQL error codes.
Library for scanning data from a database into Go structs and more.
Author: Jackc
Source Code: https://github.com/jackc/pgx
License: MIT License
1644854220
Drivers in Selenium are a vital rather a must for Browser automation. Selenium library in Python or in any other programming language uses the particular Browser’s driver to control browsers with their actions, add functionality to it, and in all manipulate data (webpage) in it.
Contrary to the most common way of declaring a Browser’s Driver which is most common, but often may result in incompatibility issues. There are multiple ways (not so common) to install, initialize web drivers in your Python code.
The foremost point of error in Selenium occurs when the browser’s driver’s version doesn’t match with the browser’s version, for which compatibility issue arises. So, to start with, you must always use the driver with the same version as that of the Web Browser(Google Chrome, Mozilla Firefox, Apple Safari, or any other) which you intend to use for automation.
Downloading and installing Web Drivers is completely safe because these drivers are maintained by the Official Browser Companies and updated accordingly, for testing and automation purposes of their browser and webpages.
Check with your browser which you intend to automate for its version and download the driver from the below references accordingly:
Web Browser | Driver Download Reference |
---|---|
Google Chrome/Chromium | Download |
Mozilla Firefox | Download |
Microsoft Edge | Download |
Apple Safari | Already built-in |
Let’s now look at how we can use the selenium web drivers.
Advantage: No need to care about lengthy setup or figuring out of Environment variable
Disadvantage: Makes the code less flexible and subject to manual changes for every update
driver = webdriver.Chrome('path/to/chromedriver")
driver = webdriver.Chrome('C://software/chromedriver.exe') #driver located at the specified location
driver = webdriver.Chrome('chromedriver.exe') #driver located in the same directory as of the python script file
#other way
service = Service(executable_path="/path/to/chromedriver")
driver = webdriver.Chrome(service=service)
Advantage: No hassle of specifying the path to the driver
Disadvantage: No update functionality
To set the Environment Path Variable, go to your command prompt and type in the following command:In place of “C:\WebDriver\bin” in the below command, use the path (install location) of the driver.
setx PATH "%PATH%;C:\WebDriver\bin"
If you find this way difficult, then you may search for Environment Variable in your Windows Start Menu and click open -“Edit the system environment variables“. After that, from the pop-up window select “Environment Variables” to open another pop-up window.
From the System Variables option, select open Path and now click on New to introduce a new path variable. Paste the location of your web driver in it, then OK, OK, and finally again OK, in all windows.
Environment Variable
The last and probably the most useful method to use is the Web Driver in your Python code. On selecting, automatic updating in Web Browser, the device only updates the browser and not the installed driver, in this case upon execution of python script the code gives error for not equal versions of the browser and driver.
Advantage: No version compatibility issues and help easily switch between multiple browsers
Disadvantage: Maybe a bit difficult to set up for beginners
Install the driver manager
pip install webdriver-manager
Import the manager
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager # --> for Chrome
#from webdriver_manager.firefox import GeckoDriverManager # --> for Firefox
Now as we have installed and imported the manager, we use it in our code like –
Use the install()
method to get the location used by the manager and pass it into a loc_service class. This method informs about the location of the installed web driver by itself.
driver = webdriver.Chrome(ChromeDriverManager().install()) # --> for Chrome
#driver = webdriver.Firefox(executable_path=GeckoDriverManager().install()) #--> for Firefox
For any other browser, you may check the official GitHub repository of the driver manager software.
That’s it for the tutorial. Contrary to the popular methods of using the web drivers in Selenium, in this tutorial you learned about the other and maybe advantageous methods to perform the same.
Link: https://www.askpython.com/python-modules/introduction-browser-drivers-selenium