COPY FROM statement
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 basic syntax for using COPY FROM STDIN
is:
Parameters:
table_name
: The table where the data will be importedstdin
: Indicates that the data comes from the standard input (client application)
- Format: Only .csv is supported
- Delimiter: For CSV format, the default delimiter is a comma (,)
- Delimiter: For CSV format, the default delimiter is a comma (,)
Additional Options
- Listing Column Names You can specify the columns into which the data should be imported.
- 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:
- 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:
- To end the import process, enter
\.
or an EOF signal.
An EOF (End of File) signal is used to indicate the end of input data and to terminate the input process. On Unix-like systems, press
Ctrl + D
.- If the import is successful, you will see
IMPORT 0
at the end of the line.
The
IMPORT 0
message doesn’t reflect the number of rows imported. This feature is planned for future development.- Verify the imported data by querying the table:
- The output should show that the data has been successfully imported into the table:
Importing a CSV File Directly
To import a CSV file directly into Oxla, use the following steps. This method bypasses the need to manually enter data by reading the file and importing it directly into Oxla.
- Create the Table (if not already created):
- Exit
psql
:
- Import the CSV File:
This command reads the contents of myfile.csv and passes it directly to the COPY
command.
- Re-enter
psql
and verify the table contents: