COPY FROM STDIN
Overview
The COPY FROM STDIN
command imports data directly from the client into a table. It simplifies the copy process by eliminating the need to transfer files to the server.
Syntax
The syntax for this function is as follows:
Parameters
table_name
: table where the data will be importedstdin
: data coming from the standard input (client application)
When it comes to file format, only CSV files are supported and the default delimiter for this format is a comma ,
.
Additional Options
1. Listing Column Names
You can specify the columns into which the data should be imported.
2. Options
You can include additional options following FROM stdin
to customize the import process.
Examples
Importing Data Manually
- Ensure the table exists in your database. If it doesn’t, create one using the following command:
You should see the output indicating that the table has been created.
- Initiate the import operation by running the following command:
- You will be prompted to enter your data. There will be a message as shown below:
-
Paste the data directly from your CSV file into the prompt:
If the import is successful, you will see IMPORT 0
at the end of the line.
- Verify the imported data by querying the table in a following way:
The output from that query should be as follows:
Direct CSV File Import
Use the following steps to import a CSV file directly into your Oxla instance. This method bypasses the need to manually enter data by reading the file and importing it directly into Oxla. After launching the psql
client application and creating the film
table, download the film-dataset.csv file and execute the following query:
- Replace
table_name
with your target table name - Replace
/path/to/file
with the full path to your CSV file - Use
HEADER
if your CSV file includes column headers
Importing Data Using cat
Method
Ensure your dataset is in a valid CSV format. After creating a table using psql
, please follow the following steps:
- Type
\q
followed byEnter
to exitpsql
- Import the CSV File:
This command reads the contents of film-dataset.csv file and passes it directly to the COPY
command.