dev-resources.site
for different kinds of informations.
How to import CSV file to the Database
Published at
12/19/2023
Categories
elixir
ecto
postgres
tutorial
Author
Herminio Torres
How to import CSV data file to the database and populate the table.
Imagine you have this migration in your application with the following columns:
create table(:users) do
add :first_name, :string, null: false
add :last_name, :string, null: false
add :username, :string, null: false
add :email, :string, null: false
end
And this would be the CSV file:
First Name,Last Name,Username,Email
John,Doe,john_doe,[email protected]
Jane,Doe,jane_doe,[email protected]
...
And how can I import my CSV file to the users' table on the database?
~$ psql -U user -d database <<USERS
COPY users(first_name, last_name, username, email) FROM '/path/to/users.csv' DELIMITER ',' CSV HEADER;
USERS
After finishing, you will receive an output with COPY 2
the number of copies in your table.
Articles
12 articles in total
Bridging the Gap: Simplifying Live Component Invocation in Phoenix LiveView
read article
Taming data with Ecto.Enum and Ecto.Type
read article
Troubleshooting MacOS Performance: Addressing fseventsd Issues Causing Memory Leaks and Sluggish Performance
read article
Exploring the Data Analysis: From Python Certification to the Elixir Challenge - Mean-Variance-Standard Deviation Calculator
read article
Using the Keyword module for options
read article
How to import CSV file to the Database
currently reading
How to take leverage from on_mount to reduce code
read article
What you should know about the live_session macro
read article
When to use the handle_params callback
read article
How to use unique_index wisely to grasp our business logic
read article
Understanding Process Restart Strategies: Transient, Temporary, and Permanent
read article
Managing Timeouts in GenServer in Elixir: How to Control Waiting Time in Critical Operations
read article
Featured ones: