PostgreSQL copy command with examples

In this article, we will see PostgreSQL copy command with examples.
PostgreSQL COPY command moves data between PostgreSQL tables and standard file-system files. COPY TO copies the contents of a table to a file, while COPY FROM copies data from a file to a table (appending the data to whatever is in the table already). COPY TO can also copy the results of a SELECT query.

COPY FROM syntax:
Continue reading PostgreSQL copy command with examples

How to create copy of existing database in PostgreSQL

In this article, we will see how to create copy of existing database in PostgreSQL.

Postgres allows the use of any existing database on the server as a template when creating a new database.

Syntax to create copy of PostgreSQL Database:
Continue reading How to create copy of existing database in PostgreSQL

How to export PostgreSQL data to csv or excel file

In this article, we will see how to export PostgreSQL data to csv or excel file. COPY command is used to copy data between a file and a table in PostgreSQL.

Syntax to export data from a table to csv or excel or any other file formats:
Continue reading How to export PostgreSQL data to csv or excel file

How to import CSV file data into PostgreSQL table

In this article, we will see How to import CSV file data into PostgreSQL table. It is nothing but loading data from a spreadsheet.

1. Create table

CREATE TABLE student(sno int primary key, sname varchar(50), sage int, dob date);

Continue reading How to import CSV file data into PostgreSQL table

How to Copy table from one database to another in PostgreSQL

In this article, we will see how to Copy table from one database to another in PostgreSQL. We can copy a table from one database to other using pg_dump tool.

Copy table from one database to another in PostgreSQL:

If table is empty then, run the below command from Linux.

pg_dump -t table_to_copy source_db | psql target_db

Continue reading How to Copy table from one database to another in PostgreSQL

How to save query result to a file in PostgreSQL

In this article, we will see how to save query result to a file in PostgreSQL. We can save query result to txt, csv,.. files.

We have three methods to save/export query results to a file in PostgreSQL:

1) copy command
2) from Linux shell using psql tool.
3) \o method.

Continue reading How to save query result to a file in PostgreSQL