Migrating from PostgreSQL to SurrealDB
This page details some common PostgreSQL patterns and their SurrealQL equivalents, followed by links to the Surreal Sync tool which allows data from PostgreSQL to be automatically imported to SurrealDB.
Data types
The following chart shows PostgreSQL data types along with the equivalent or near-equivalent SurrealQL data type for each.
| PostgreSQL Data Type | Wire Protocol Type | SQL Representation | SurrealDB Mapping | Notes |
|---|---|---|---|---|
| BOOLEAN | Boolean | true/false | bool | |
| SMALLINT | Int2 | 32767 | int | |
| INTEGER | Int4 | 2147483647 | int | |
| BIGINT | Int8 | 9223372036854775807 | int | |
| SERIAL | Int4 | 1, 2, 3... | int | Auto-increment converted to regular integer |
| BIGSERIAL | Int8 | 1, 2, 3... | int | Auto-increment converted to regular integer |
| REAL | Float4 | 3.14 | float (f64) | Converted to double precision |
| DOUBLE PRECISION | Float8 | 3.141592653589793 | float (f64) | |
| NUMERIC/DECIMAL | Numeric | 123.45 | number | Converted to SurrealDB Number with exact precision preserved |
| MONEY | Money | $123.45 | number | Currency symbol removed, converted to number |
| CHAR(n) | Bpchar | 'text' | string | Fixed-length, padding removed |
| VARCHAR(n) | Varchar | 'text' | string | Variable-length string |
| TEXT | Text | 'long text' | string | Unlimited length string |
| BYTEA | Bytea | \\x48656c6c6f | bytes | Binary data, hex decoded |
| DATE | Date | '2024-01-15' | datetime | Converted to datetime at midnight UTC |
| TIME | Time | '14:30:00' | string | Time-only as string (SurrealDB has no pure time type) |
| TIMESTAMP | Timestamp | '2024-01-15 14:30:00' | datetime | Converted to UTC datetime |
| TIMESTAMPTZ | Timestamptz | '2024-01-15 14:30:00+00' | datetime | Timezone-aware, converted to UTC |
| INTERVAL | Interval | '1 day 2 hours' | duration | Converted to SurrealDB duration |
| UUID | Uuid | '550e8400-e29b-41d4-a716-446655440000' | string | UUID string representation |
| JSON | Json | '{"key": "value"}' | string | JSON stored as string representation |
| JSONB | Jsonb | '{"key": "value"}' | string | Binary JSON stored as string representation |
| ARRAY | Array | '{1,2,3}' | array | Recursively processed, element types converted |
| POINT | Point | '(1.5, 2.5)' | object | Convert to {"x": 1.5, "y": 2.5} object |
| LINE | Line | '{1,2,3}' | object | Convert to coefficient object |
| LSEG | Lseg | '[(1,2),(3,4)]' | object | Line segment as start/end point object |
| BOX | Box | '(1,2),(3,4)' | object | Bounding box as corner points object |
| PATH | Path | '[(1,2),(3,4)]' | array | Array of point objects |
| POLYGON | Polygon | '((1,2),(3,4),(5,6))' | array | Array of point objects |
| CIRCLE | Circle | '<(1,2),3>' | object | Center point and radius object |
| INET | Inet | '192.168.1.1' | string | IP address as string |
| CIDR | Cidr | '192.168.0.0/24' | string | Network address as string |
| MACADDR | Macaddr | '08:00:2b:01:02:03' | string | MAC address as string |
Inserting data
Defining a schemafull table
A table in PostgreSQL might be defined as follows.
In SurrealQL, a table does not by default need to be defined before it can be used. However, the following statements will produce a strict schema similar to the PostgreSQL one above.
One difference between this and the PostgreSQL schema above is that a product will have a randomly generated ID as opposed to an incrementing one.
Selecting data
Selecting records using an ID:
Selecting multiple specific records:
Counting the number of records in a table:
Queries with identical syntax
As the SurrealQL is inspired by SQL, many queries between it and PostgreSQL are identical.
Using record ID instead of the WHERE clause
If a record ID is known ahead of time and you are using a version of SurrealDB before 3.0, be be sure to query by the record ID itself instead of using a WHERE clause in SurrealQL. This will avoid a full table scan if the field is not indexed.
Joining and querying related tables
Take the following query with joins in PostgreSQL:
In SurrealQL, tables can be joined to each other via edges, such as the bought edge in this example.
Once the tables have been related (joined), they can be queried with this syntax.
An example of more complex query with joins to return all people who bought the same products as a certain customer (including the original customer).
Importing from PostgreSQL using Surreal Sync
For more on how to import data from PostgreSQL to SurrealDB, please see the following pages in the Surreal Sync repo.