Sync Your Postgres Data with ElectricSQL: A Step-by-Step Guide

Jul 17, 2021 | Programming

Welcome to the world of ElectricSQL, where your Postgres database can stay in perfect harmony across various platforms through a seamless syncing process. This guide will walk you through the steps needed to set up and utilize this powerful tool, along with troubleshooting ideas to keep your experience smooth.

What is ElectricSQL?

ElectricSQL offers an HTTP API that facilitates the synchronization of data shapes from your Postgres database. It’s like adding a magic wand to your database, allowing it to flow effortlessly to wherever you need it! With Electric, not only can you interact via an API, but you also have the option of using client libraries for various integrations like React.

Why the Change?

You might notice that ElectricSQL feels a bit different than the last time you visited. This is due to a complete redesign of the sync engine that started in July 2024, optimized based on the experiences gained from the previous iteration. If you’re curious about the transition, you can take a look at James’ blog post that dives into the details of this evolution.

Getting Started with ElectricSQL

Ready to dive in? Here’s a quick guide to get you started:

  1. Ensure you have a Postgres database with logical replication enabled.
  2. Run ElectricSQL in front of it, connected via DATABASE_URL.

For instance, here’s how you can start using Docker Compose to bring it to life:

sh
docker compose -f .support/docker-compose.yml up

Once you’re up and running, you can utilize the HTTP API to sync data efficiently.

Syncing Data Example

To sync an entire table, you might use the following command:

sh
curl -i http://localhost:3000/v1/shape/foo?offset=-1

Alternatively, if you’re working with React, you can employ the useShape hook like so:

jsx
import useShape from '@electric-sql/react';

function Component() {
   const data = useShape({
      url: 'http://localhost:3000/v1/shape/foo',
      where: 'title LIKE "foo%"',
   });
   return JSON.stringify(data);
}

For detailed guidance, don’t forget to check the Quickstart guide and the Docs.

Developing with ElectricSQL

If you’re interested in contributing to ElectricSQL, the project uses asdf for managing version control for Elixir, Erlang, and Node.js. Here’s a quick setup guide for Mac users:

sh
brew install asdf
asdf plugin-add nodejs
asdf plugin-add elixir
asdf plugin-add erlang
asdf install

You may need to adjust your shell configuration file to load the right environment settings as well.

Troubleshooting

Encountering issues? Here are some common troubleshooting tips:

  • Ensure that your Postgres database is running and accessible.
  • Double-check the DATABASE_URL configuration to confirm it correctly points to your database.
  • If errors persist, check your logs for any client connection issues.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

With ElectricSQL, syncing your Postgres data can be as simple as flipping a switch. Dive in and explore the possibilities! By ensuring the proper setup, you can harness the power of your database throughout various platforms without a hitch.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox