Skip to main content

Google Sheets to Postgres DB using gspread and env files.

Project description

g2pg

g2pg takes a Google Sheet, converts it to a DataFrame, which you can then manipulate as you need to.

This DataFrame can then be written to a PostgreSQL database table.

This makes use of a .env file, which after much trial and error I finally got to work with gspread.

Can install using pip install g2pg.

To use:

import g2pg

How does it work

g2pg uses the gspread package to extract data from the Google Sheet.

Follow these directions to get the json credentials file that can be used with gspread https://gspread.readthedocs.io/en/latest/oauth2.html#for-bots-using-service-account

Make sure you share any Gsheets with the email that is specified in your credentials file.

The json credentials need to be stored in a .env file. Don't upload the contents of your .env or json_credentials file to github unless you want everyone to know your secrets.

If using this package you need to have a .env file or enviroment variables set in the following way.

DB_USER= 'username'
DB_PW = 'super_secret_password'
DB_URL = 'db_address'
DB_NAME = 'db_name'

SHEET_TYPE= 'service_account'
SHEET_PROJECT_ID= 'api-project-XXX'
SHEET_PRIVATE_KEY_ID= '2cd … ba4'
SHEET_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nNrDyLw … jINQh/9\n-----END PRIVATE KEY-----\n"
SHEET_CLIENT_EMAIL= 'yoursisdifferent@developer.gserviceaccount.com'
SHEET_CLIENT_ID= '1234567890'
SHEET_AUTH_URI= 'https://accounts.google.com/o/oauth2/auth'
SHEET_TOKEN_URI= 'https://oauth2.googleapis.com/token'
SHEET_AUTH_PROVIDER_X509_CERT_URL= 'https://www.googleapis.com/oauth2/v1/certs'
SHEET_CLIENT_X509_CERT_URL= 'https://www.googleapis.com/robot/v1/metadata/bla...bla..bla.iam.gserviceaccount.com'

There are 2 methods available:

  • def get_df_from_gsheet(gsheet_name,worksheet_name) This returns a datframe from the specified Google Sheet and worksheet.The worksheet name is optional and will default to Sheet1. The dataframe will have all columns and rows removed, where there is no data, and the column names will be converted to a database friendly format.
  • def df_to_db(df, table_name,schema, index_name) This writes the specified df to the table_name in the DB that is specified in the .env file. schema is optional and will default to public in postgres if not specified. index_name is the index of your df. If not specified, it will default to index (this is used as the primary key in your DB table).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

g2pg-1.3.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

g2pg-1.3.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file g2pg-1.3.0.tar.gz.

File metadata

  • Download URL: g2pg-1.3.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.3

File hashes

Hashes for g2pg-1.3.0.tar.gz
Algorithm Hash digest
SHA256 90f05e9cff7d1f22d2b7678a1c06818d4d9d1defcf97145502e4dcd5368efcda
MD5 ecb3c04669804d8f0390f485bf539357
BLAKE2b-256 4854378e03a5fe8bac695833184c203bb71458aa400039e39d80781da4f6f731

See more details on using hashes here.

File details

Details for the file g2pg-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: g2pg-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.3

File hashes

Hashes for g2pg-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 564b4540e6dc70c79a642219d6a1264f05dd1e22933e4728d616d857ef170df0
MD5 0ec426cc73bb2570a5b0df77c720b1eb
BLAKE2b-256 ecbaa15c701c27e0c540ff2c6b6f0e9c1860e242b63f03ff93c82bbb245f09c0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page