The COPY Command
At this point, you are probably familiar with the SELECT
statement (covered in Chapter 2, The Basics of SQL for Analytics), which allows you to retrieve data from a database. While this command is useful for small datasets that can be scanned quickly, you will often want to save a large dataset to a file. By saving these datasets to files, you can further process or analyze the data locally using Excel or Python. To retrieve these large datasets, you can use the PostgreSQL COPY
command, which efficiently transfers data from a database to a file, or from a file to a database. This COPY
command must be executed when connected to the PostgreSQL database using a SQL client, such as the PostgreSQL psql
command. In the next section, you will learn how to use the psql
command, then you will learn how to copy data with it.
Running the psql Command
You have been using the pgAdmin
frontend client to access your PostgreSQL database, and you have briefly used the psql
tool...