By Rudderstack Team

How to load data from PostgreSQL to Google BigQuery

This post will help you to load your data stored in PostgreSQL to Google BigQuery. You will perform advanced analysis on a system that is dedicated to analytics and is suitable for this kind of data payload, like Google BigQuery, without worrying about interfering with the production setup.

Generally speaking, replicating a PostgreSQL to any other database system is not a trivial task. Exporting a PostgreSQL database into CSV files using the pg_dump command and loading them into any other system is not enough. You also need to take care of the loading of new or updated data using a CRON job that will constantly trigger the execution of a script that will check for new updates in your tables. However, especially in cases where latency is an important factor, this process can be extremely slow.


Also, depending on the selected destination, the data loading process can be significantly different. Alternatively, you can simplify the process of syncing data from PostgreSQL to Google BigQuery by using RudderStack, which does all the heavy lifting in just a few clicks so that you can focus on what matters - data exploration and the analysis.


Access your data on PostgreSQL

The first step in migrating your PostgreSQL data to any data warehouse solution is accessing and extracting it. There are many ways of doing this like, for example, logical replication log as previously mentioned.

You need to listen to the log for changes on the database and reflect them on the target system. When pulling data from a database, you also need to filter tables, columns, find a way to identify updates and replicate the appropriate database schema considering that it will end up in a columnar database for analytics.

Another way is by using a JDBC importer. In this case, any input configuration will contain all the appropriate values for the database authentication and connection. By appropriately configuring the JDBC importer, you can control each table's behavior during import and alter its schema as well, if desired. Moreover, pagination of data importing can be simulated by querying tables in batching mode.

Transform and prepare your PostgreSQL data

After you have accessed your data on PostgreSQL, you will have to transform it based on two main factors:

  1. The limitations of the database that you will load the data into
  2. The type of analysis that you plan to perform


Each system has specific limitations on the data types and data structures that it supports. Also, you have to choose the right data types. Again, depending on the system you will send the data to, you will have to make the right choices.

For the most common data types, the mapping choices may seem to be obvious. Each database system will mostly support a set of more sophisticated and database-specific types whose mapping choices require careful consideration. This factor is quite important because it can limit your queries' expressivity directly out of the database.

However, plan to push the data to another PostgreSQL database. You probably don't have to worry about the data types unless you have some reasons for the analysis you will perform.


About PostgreSQL

No matter your company's product, the chances are your production PostgreSQL database will hold almost all of the valuable data.

This data needs to be further analyzed from the users' interaction with your platform or e-shop to product descriptions and user-generated events. This analysis is required to identify the aspects of the product you need to focus on and the most commonly faced problems related to extra traction and more users.

You will also want to enrich this data with other data from your ticketing system, your accounting software, or your marketing platform.

Having all the data stored in a database dedicated to performing analytics, you can:

  1. Generate on-going reports by calculating the key performance indicators that matter the most to you
  2. Develop and test predictive applications aiming to automate the company's core business tasks
  3. Connect with your preferred business intelligence tool and create interactive dashboards to communicate the insights you have derived across all teams.

Load data from PostgreSQL to Google BigQuery

If you want to load PostgreSQL data to Google BigQuery, you have to use one of the following supported data sources:

  1. Google Cloud Storage
  2. Sent data directly to BigQuery with a POST request
  3. Google Cloud Datastore Backup
  4. Streaming insert
  5. App Engine log files
  6. Cloud Storage logs

From the above list of sources, 5 and 6 are not applicable in our case.

For Google Cloud Storage, you first have to load your data into it; there are a few options on how to do this. For example, you can use the console directly as described here, and do not forget to follow the best practices.

Another option is to post your data through the JSON API. As we see again, APIs play an important role in both the extraction and the loading of data into our data warehouse. It's just a matter of one HTTP POST request using a tool like CURL or Postman in its simplest case.

After you have loaded your data into Google Cloud Storage, you have to create a Load Job for BigQuery to load the data into it. This job should point to the source data in Cloud Storage that has to be imported. You can do this by providing source URIs that point to the appropriate objects.

The best way to load data from PostgreSQL to Google BigQuery and possible alternatives

In this post, we just scraped the surface of what can be done with Google BigQuery and how to load data into it. The way to proceed relies heavily on the data you want to load, the service from which it is coming from, and your use case's requirements. However, things can get even more complicated if you want to integrate data coming from different sources.

Instead of writing, hosting, and maintaining a flexible data infrastructure, a possible alternative is to use a product like RudderStack that can handle this kind of problem automatically for you.

RudderStack integrates seamlessly with multiple sources or services like databases, CRM, email campaigns, analytics, and more. Now you can quickly and safely move all your data from PostgreSQL into Google BigQuery and start generating insights from your data in no time.

Easily sync your product data from PostgreSQL into your Analytics data warehouse without affecting your live product system and combine it with your Sales, Marketing, Support, and Finance data to transform your business insights.

Easily sync your product data from PostgreSQL into your Analytics datawarehouse without affecting your live product system and combine it with your Sales, Marketing, Support, and Finance data to transform your business Insights

RudderStack is the easiest way to automate powerful data integrations.

Try RudderStack Free Today