How can I link a Google spreadsheet to PostgreSQL?

后端 未结 5 1663
南方客
南方客 2020-12-29 11:30

How can I link a Google spreadsheet to PostgreSQL? I googled and got some sample code for MySQL, and a couple of products which do it. As per this link ,support exists for a

5条回答
  •  长发绾君心
    2020-12-29 12:03

    We have been pulling Google Sheets into QGIS via PostgreSQL Foreign Data Wrappers. We then build a materialized view that connects records to geometry (via School Numbers, for example) and use the materialized view as a standard spatial table.

    From there we have also published this 'google sheet materialized view' to a web application via node.js and add a button to refresh the map if the google sheet data has been changed. It works really well.

    Method 1: Multicorn FDW and GSheets Extension:

    (Note: The gsspreadsheet extension to the multicorn FDW is questionably maintained, and there is the risk that v4 of the GSheets API may break it... we don't know yet, and are planning for the possibility that we might have to implement Method 2 (below) if it does indeed break. )

    To connect to a Google Sheet via a PostgreSQL FDW, use the Multicorn FDW:

    https://github.com/Kozea/Multicorn

    Then install the gspreadsheet_fdw extension for Multicorn:

    https://github.com/lincolnturner/gspreadsheet_fdw

    In PostgreSQL, create the multicorn FDW:

    CREATE SERVER multicorn_gspreadsheet
        FOREIGN DATA WRAPPER multicorn
        OPTIONS (wrapper 'gspreadsheet_fdw.GspreadsheetFdw');
    

    Then create the FDW table connecting to the Google Sheet using the gspreadsheet extension of multicorn:

    CREATE FOREIGN TABLE speced_fdw.centerprogram_current_gsheet (
        column1 integer NULL,
        column2 varchar NULL,
        column3 varchar NULL 
    )
    SERVER multicorn_gspreadsheet
    OPTIONS (keyfile '/usr/pgsql-9.5/share/credential.json', gskey 'example_d33lk2kdislids');
    

    You now have a foreign table pointing directly to your Google Sheet, which you can built a materialized view from.

    Method 2: Using FILE_FDW and CSV GSheet Export:

    The other option is to use the FILE_FDW right out of PostgreSQL and connect to the CSV export of the GSheet using WGET.

    First, create the server:

    CREATE SERVER fdw_files
        FOREIGN DATA WRAPPER file_fdw
        OPTIONS ()
    

    Then create the FDW table:

    CREATE FOREIGN TABLE public.test_file_fdw (
        "name" varchar NOT NULL,
        "date" varchar NULL,
        "address" varchar NULL
    )
    SERVER fdw_files
    OPTIONS (program 'wget -q -O - "https://docs.google.com/spreadsheets/d/2343randomurlcharacters_r0/export?gid=969976&format=csv"', format 'csv', header 'true');
    

    The above options for wget are listed here: https://www.gnu.org/software/wget/manual/html_node/HTTP-Options.html

提交回复
热议问题