Skip to content

Commit

Permalink
docs: add a guide on importing data from a CSV file into PG (close ha…
Browse files Browse the repository at this point in the history
…sura#494)

GITHUB_PR_NUMBER: 6225
GITHUB_PR_URL: hasura#6225

Co-authored-by: Funmilayo E. Olaiya <35759534+codeliezel@users.noreply.github.com>
Co-authored-by: Rikin Kachhia <54616969+rikinsk@users.noreply.github.com>
GitOrigin-RevId: 232414f
  • Loading branch information
3 people committed Feb 2, 2021
1 parent bcb1ae0 commit 60613b4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
65 changes: 65 additions & 0 deletions docs/graphql/core/guides/postgres/import-data-from-csv.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.. meta::
:description: import data from csv into postgres
:keywords: hasura, docs, postgres, import, data

.. _postgres_import_data_from_csv:

Import data from CSV into Postgres
==================================

.. contents:: Table of contents
:backlinks: none
:depth: 1
:local:

Introduction
------------

You might have existing data stored in a CSV file that you need to import into your Postgres database. The following
guide will show how to do so.

Let's assume we have the following CSV file, which is named ``profile.csv``:

.. thumbnail:: /img/graphql/core/guides/sample-data-csv-file.png
:alt: .csv data file
:width: 500px

Step 1: Add a corresponding table to your PG database
-----------------------------------------------------

Let us create a table to match the data structure in your CSV file.

.. code-block:: sql
profile (
firstName TEXT,
lastName TEXT,
email VARCHAR
)
Step 2: Connect to your Postgres database
-----------------------------------------

Connect to your Postgres database by using the ``psql`` command on the terminal:

.. code-block:: bash
psql postgres://<username>:<password>@<host>:<port>/<database>
# for example
psql postgres://postgres:postgres@localhost:5432/postgres
Step 3: Import the data from the CSV file
-----------------------------------------

Once connected to the database, use the following command from inside ``psql`` to
import the data:

.. code-block:: bash
\copy <table_name> from '</path/to/file/filename.csv>' delimiter ',' CSV HEADER;
# for example
\copy profile from '/Users/sarahlewis/documents/profile.csv' delimiter ',' CSV HEADER;
Your data would have been successfully copied into the Postgres database.
2 changes: 2 additions & 0 deletions docs/graphql/core/guides/postgres/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The following are some Postgres features that can be used to manage your data an
- :ref:`Functions <postgres_functions>`
- :ref:`Triggers <postgres_triggers>`
- :ref:`Indexes <postgres_indexes>`
- :ref:`Import data from CSV <postgres_import_data_from_csv>`

.. toctree::
:maxdepth: 1
Expand All @@ -55,3 +56,4 @@ The following are some Postgres features that can be used to manage your data an
Functions <functions>
Triggers <triggers>
Indexes <indexes>
Import data from CSV <import-data-from-csv>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 60613b4

Please sign in to comment.