5th Feb 2021 Updated: 28th Oct 2024 8 minutes read How to Import CSVs to PostgreSQL Using PgAdmin Jakub Romanowski PostgreSQL Table of Contents What Is a CSV file? Why Import CSV Files into a Database? PostgreSQL and pgAdmin How to Import CSVs to PostgreSQL 1. Create a Table in pgAdmin 2. Import the CSV file Troubleshooting and Limitations Well, Importing a CSV file to a Postgres Database Was Easy! Do you work with data and use CSV files? Here is a practical guide on how to import a CSV file 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. PgAdmin makes it easy to import and export data, ensuring seamless data transfer between different platforms. 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, and HeidiSQL. 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 to import CSV file. 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. Troubleshooting and Limitations While importing CSV files into a PostgreSQL database, you might encounter some common issues. Here are some troubleshooting tips and limitations to keep in mind: Delimiter issues: If your CSV file uses a delimiter other than a comma, such as a semicolon or a tab, make sure to specify the correct delimiter in your import command. This ensures that the data is parsed correctly. Quote issues: If fields in your CSV file are enclosed in quotes, ensure that these quotes are properly escaped in the import command. This prevents errors during the import process. Data type issues: Mismatched data types between the CSV file and the PostgreSQL table can cause the import to fail. Double-check that the data types in your CSV file align with those in the PostgreSQL table to avoid this issue. Large file sizes: Importing very large CSV files can be time-consuming and may fail due to memory constraints. Consider splitting large files into smaller chunks or using tools that can handle large data sets more efficiently. Character encoding issues: If your CSV file uses a different character encoding, such as UTF-16, ensure that the correct encoding is specified in the import command. This prevents data corruption and ensures that all characters are imported correctly. By being aware of these potential issues and knowing how to address them, you can troubleshoot effectively and ensure a successful import process. 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're just starting out with PostgreSQL, I highly recommend beginning with our SQL Basics in PostgreSQL course or exploring SQL from A to Z in PostgreSQL, our comprehensive main learning track for this powerful database language. These interactive courses are designed to make learning SQL straightforward and enjoyable. You’ll start with the basics, like creating tables and querying data, and move through more advanced topics, building up your skills step-by-step. By the end, you'll have all the knowledge and confidence you need to work effectively with databases—whether you’re preparing for a new job, enhancing your current role, or just satisfying your curiosity. PostgreSQL is quickly becoming one of the most sought-after database management systems in the tech world. It’s already widely used by big companies, startups, and even in academia, all betting on PostgreSQL’s flexibility, scalability, and reliability. Learning it now could open doors to countless opportunities, as more industries turn to PostgreSQL for their data needs. So why not start learning PostgreSQL today? With these courses, you’ll gain practical, hands-on experience that will set you apart. Give it a try and see where your new SQL skills can take you! Tags: PostgreSQL