Back to articles list February 5, 2021 - 7 minutes read How to Import CSVs to PostgreSQL Using PgAdmin Jakub Romanowski Jakub was a journalist for various news television stations for many years. Then, he fell in love with programming, and it changed his life. At Vertabelo, he attracts and retains clients through content marketing. In his free time, he enjoys playing football with his son, reading science fiction, training in martial arts, and cooking for his family and friends. Tags: sql learn sql PostgreSQL Do you work with data and use CSV files? Here is a practical guide on how to import such files into a PostgreSQL database using pgAdmin, one of the best PostgreSQL editors on the market. Let's get right into importing CSVs into a Postgres database. We’ll start by explaining what a CSV file is, then we’ll introduce you to pgAdmin and show you how to do the import process. Don’t worry – it’s easy! What Is a CSV file? CSV is short for comma-separated values. It is a format for storing data in text files. This standard is supported by many applications and programs, including Microsoft Office, LibreOffice, and Google Sheets. Why Import CSV Files into a Database? When are CSV files used? Transferring data from one program or platform to another is a main use. Imagine that you need to export data from a database to a spreadsheet or vice versa. CSVs are the surest way to save your data; they are read by almost all office suites and database management systems (DBMSs). In addition, CSVs are text files and thus are quite small; transferring even large data sets is not a problem. That’s a huge advantage to using CSVs. Have a look at the differences between storing data in a table and storing it in a CSV. Here is a table called characters. I wonder if you recognize the data I put there. IDfirst_namelast_namefamily 1JohnSnowTargaryen 2AryaStarkStark 3TyrionLannisterLannister Here’s the same information in CSV format: ID,first_name,last_name,family 1,John,Snow,Targaryen 2,Arya,Stark,Stark 3,Tyrion,Lannister,Lannister This is a very tiny data set, but you can imagine what it would be like with many columns and thousands of rows. I think you understand the uses and advantages of CSVs, so let’s move on to the tool we’ll use to import them: pgAdmin. PostgreSQL and pgAdmin Some time ago, my friend Ignacio showed you how to install and set up PostgreSQL on Windows 10. If you’ve read that article, you know that Postgres is easy to set up and one of the most popular DBMSs in the world. There are tons of different SQL editors on the market that support PostgreSQL. I got used to pgAdmin and honestly I don't see any point in changing to anything else. It's a great tool that has never let me down. What is pgAdmin? It is a graphical PostgreSQL database management program. Its latest version is pgAdmin 4. It may be used on Linux, Unix, macOS, or Windows, and because it is open-source, it's free. This will be an important argument for many people. Full functionality and no fees make pgAdmin perfect for me. And maybe for you, too. PgAdmin allows you to administer databases and their replicas, create all database objects, issue queries, and analyze the query plan (presented in classic or graphic form). You can also use the classic console to write in it. Plus, there are many extensions that allow you to freely adapt the program to your needs. Alternatives to pgAdmin include DBeaver, Navicat, HeidiSQL, and OmniDB. If you would like me to devote an article to the various SQL editors out there – including those for PostgreSQL databases – let me know in the comments. I assume that you already have Postgres and pgAdmin installed on your computer and that you have them set appropriately. Now we can get down to business and import some data. How to Import CSVs to PostgreSQL Let's go back to our characters.csv file and try to import it into our database via pgAdmin. There are two methods, and I will show you what to do step by step. 1. Create a Table in pgAdmin Using the internal Query Tool in pgAdmin, you can first create a table with an SQL query: CREATE TABLE characters (id serial, first_name varchar, last_name varchar, family varchar ); Make sure the columns have the same names and values as those in the CSV file. Instead of writing a SQL Query, you can also right-click on Tables in the tree on the left. A short menu will appear. Select Create and then Table.... A new window will pop up asking you to enter a name for the new table. In our case, it will be characters. In the same window, click on the Columns tab and add the appropriate columns by clicking the + button. Remember that column names must match those in the CSV file. You’ll also need to specify the data type that each column will contain. If you don't know what data types to enter, I recommend taking the LearnSQL.com Data Types in SQL course. It's better to learn the basics and avoid making mistakes. We know what data we want to include in each column, so everything should look like this: Notice that you can set the column to be non-NULL and specify a primary key here as well. By clicking SQL, you can view the SQL query used to create the table. For now, we won’t worry about the other (more advanced) features here. When you are done, click Save. You just created a table! If you don't see it in the tree, right-click on Tables and select Refresh. It should appear. 2. Import the CSV file Again, you can use the Query Tool and type an SQL query. Use the COPY statement for this: COPY characters FROM 'C:\a\characters.csv' DELIMITER ',' CSV HEADER; In the first line, enter the name of your table. In the second line (after the FROM clause), enter the path to your file. In my case, it is the C drive and the a folder. Important: you need to specify the DELIMITER, which is the character used to separate the values in the CSV file. It’s usually a comma, but it can be a semi-colon, a space, or something else. Finally, use the HEADER keyword to skip the header (i.e the row containing the column names) when copying data from the CSV. In our case, the result is a COPY 3 message. This means that pgAdmin copied three rows into our table. Let’s enter a simple query to check that everything worked: SELECT * FROM characters; Of course, as in the case when creating a table as well as importing data from a CSV file, we can skip the query and use the program itself. To do this, simply right-click on your table in the tree on the left and select the Import/Export… menu item. A window will appear with the slider set to Import. Then select the source file and set the format to CSV. Set the Header to Yes if your file has a header. The only thing left is to select the delimiter (usually a comma). Your window should look like this: When you click OK, the data will be imported. And your table is ready! Now you can write SQL queries and analyze data, combine data from other tables, etc. Welcome to the other side of the mirror, Alice! You successfully followed the White Rabbit. Well, Importing a CSV file to a Postgres Database Was Easy! That’s how simple it is to import data from a CSV file into a PostgreSQL database with pgAdmin. There are two ways you can do this – by using the program itself, and by using SQL in the Query Tool and the COPY statement. I use pgAdmin because it's a great tool for working with PostgreSQL. What IDE are you using? Tell us in the comments. If you are a complete beginner to PostgreSQL, I encourage you to take our SQL Basics in PostgreSQL course or SQL from A to Z in PostgreSQL (the main track for this dialect). These are brilliant, interactive SQL courses that will teach you everything you need to work effectively with databases. Don't fall behind; the world is betting on PostgreSQL, including lots of big companies. Why not give it a try? Tags: sql learn sql PostgreSQL You may also like How to Install PostgreSQL on Windows 10 in 5 Minutes It may look like a complicated task to install database software on your PC. In this article, I’ll show you how to install PostgreSQL on Windows 10 easily. Read more Top 10 Websites That Will Help You Find the Perfect SQL Job Want to get an SQL job but don't know where to look? Here are the top 10 websites that will help you find the perfect fit. Read more Stories of the LearnSQL Scholarship Program During Christmas 2020, we decided to do something positive. 50 people received a SQL scholarship and a chance to change their lives! Read more Subscribe to our newsletter Join our weekly newsletter to be notified about the latest posts.