Home  >  Article  >  Backend Development  >  How to Efficiently Import Rows to PostgreSQL from STDIN?

How to Efficiently Import Rows to PostgreSQL from STDIN?

Linda Hamilton
Linda HamiltonOriginal
2024-10-26 12:34:02239browse

How to Efficiently Import Rows to PostgreSQL from STDIN?

How to Import Rows to PostgreSQL from STDIN

Importing rows to PostgreSQL from STDIN can be a useful technique for bulk loading data. In Go, this can be achieved using the github.com/lib/pq package.

The Python script provided in the question utilizes the subprocess module to invoke psql and specify the COPY command with the --set=ON_ERROR_STOP=false option to handle any errors during the import process.

Go Implementation

The Go code provided in the question initially writes the data to a file and then imports it into PostgreSQL. However, this method is inefficient and involves creating and deleting temporary files.

A more efficient approach is to use the pq package's CopyIn() function to directly import data from STDIN. Here is the modified Go code:

<code class="go">package main

import (
    "database/sql"
    "fmt"
    "log"

    "github.com/lib/pq"
)

func main() {
    // Open the database connection.
    db, err := sql.Open("postgres", "dbname=postgres user=postgres password=postgres")
    if err != nil {
        log.Fatalf("open: %v", err)
    }
    defer db.Close()

    // Prepare the COPY statement.
    stmt, err := pq.CopyIn(db, "test", "first_name", "last_name")
    if err != nil {
        log.Fatalf("prepare: %v", err)
    }

    // Create some sample data.
    records := [][]string{
        {"Rob", "Pike"},
        {"Ken", "Thompson"},
        {"Robert", "Griesemer"},
    }

    // Iterate over the records and write them to STDIN.
    for _, r := range records {
        _, err = fmt.Fprintf(stmt, "%s\t%s\n", r[0], r[1])
        if err != nil {
            log.Fatalf("exec: %v", err)
        }
    }

    // Signal the end of the COPY statement.
    _, err = stmt.Exec()
    if err != nil {
        log.Fatalf("exec: %v", err)
    }

    // Close the COPY statement.
    err = stmt.Close()
    if err != nil {
        log.Fatalf("stmt close: %v", err)
    }

    // Commit the transaction.
    if err = db.Commit(); err != nil {
        log.Fatalf("commit: %v", err)
    }

    fmt.Println("Data imported successfully.")
}</code>

Performance Comparison

Using this method, importing 1 million records takes approximately 2 seconds on average. This is significantly faster than writing to a file and then importing from disk.

The above is the detailed content of How to Efficiently Import Rows to PostgreSQL from STDIN?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn