forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add a guide on importing data from a CSV file into PG (close ha…
…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
1 parent
bcb1ae0
commit 60613b4
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
docs/graphql/core/guides/postgres/import-data-from-csv.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.