Skip to main content
Version: 2.0.0

Migrate PostgreSQL database to Memgraph

Prerequisites​

  • A running PostgreSQL instance with the database you wish to migrate.
  • A running Memgraph instance where you want to migrate the data.
  • The mgmigrate tool installed. Installation instructions can be found here.

How to migrate data from PostgreSQL to Memgraph?​

Dataset​

For this tutorial, we will be working with a PostgreSQL database named users_db that contains two tables, users and user_relationships:

The users table contains four users with their ids and names:

users_db=# SELECT * FROM "users";
id | name
----+------
0 | Anna
1 | Josh
2 | Lisa
3 | Troy

Migrating​

1. You can migrate this database into Memgraph by running:

build/src/mgmigrate --source-kind=postgresql /
--source-host 127.0.0.1 /
--source-port 5432 /
--source-username postgres /
--source-password postgres /
--source-database=users_db /
--destination-host 127.0.0.1 /
--destination-port 7687 /
--destination-use-ssl=false

2. Run the following query in Memgraph Lab or mgconsole to see the results:

MATCH (n)-[r]-(m) RETURN n,r,m;

The query results should be:

memgraph> MATCH (n)-[r]-(m) RETURN n,r,m;
+--------------------------------+--------------------------------+--------------------------------+
| n | r | m |
+--------------------------------+--------------------------------+--------------------------------+
| (:users {id: 1, name: "Josh"}) | [:user_relationships] | (:users {id: 0, name: "Anna"}) |
| (:users {id: 0, name: "Anna"}) | [:user_relationships] | (:users {id: 1, name: "Josh"}) |
| (:users {id: 3, name: "Troy"}) | [:user_relationships] | (:users {id: 2, name: "Lisa"}) |
| (:users {id: 2, name: "Lisa"}) | [:user_relationships] | (:users {id: 3, name: "Troy"}) |
+--------------------------------+--------------------------------+--------------------------------+