Atikh's DBA blog
  • Home
  • Oracle
  • MySQL
  • MongoDB
  • PostgreSQL
  • Snowflake
  • About Me
  • Contact Us

Postgres spooling file and other command line options

 Atikh Shaikh     PostgreSQL     3 comments   

I wanted to drop all the tables in a particular postgres database, there were around 90 tables, dropping individual tables one by one is not a good idea and is time-consuming as well, 
So decided to create a DROP script and then execute it, the challenge for me was to spool the file in PSQL as it is slightly different from SQL language
In the below example I have spooled drop statements in the file and then executed the same by login to the postgres database

Login to database tech_db using tech_usr 


$ psql -d tech_db -U tech_usr

Password for user tech_usr:

psql.bin (9.6.2.7)

Type "help" for help.

tech_db=#


Check the connection information

 

tech_db=# \conninfo

You are connected to database "tech_db" as user "tech_usr" via socket in "/tmp" at port "5442".

tech_db=#

Check the tables in the database


tech_db=# \dt

                        List of relations

 Schema |             Name              | Type  |      Owner

--------+-------------------------------+-------+-----------------

 public | admin_events                        | table | tech_db

 public | associat_policy               | table | tech_db

 ...

 ...

 ...

 public | origins_tab                   | table | tech_db

(90 rows)

Create Drop script for drop tables 


tech_db=# \o drop_tables.sql

tech_db=# select 'drop table if exists "' || tablename || '" cascade;'   from pg_tables where schemaname = 'public';

tech_db=# \o

you will be able to see file drop_tables.sql is created under current directory


$ ls -lsrth
8.0K -rw-r--r-- 1 technodba technodba 4.9K Jan 24 08:42 drop_tables.sql

Execute script in PSQL command prompt


tech_db-# \o drop_tables.log

tech_db-# \i drop_tables.sql

tech_db-# \o

Now check the tables 


tech_db=# \dt

No relations found.

tech_db=#

Playing with the command line
As we see, by default query wont be written to output file, we have to specifically mention to write query to output file,
this is is how we can do it. We need to use 
\qecho before query

tech_db=# \o tables.sql

tech_db=# select * from origins_tab;

tech_db=# \qecho select * from origins_tab;

tech_db=# \o

tech_db=# \q

$

$ ls -lsrth

total 4.0K

4.0K -rw-r--r-- 1 technodba technodba 77 Jan 28 04:02 tables.sql

$

$ cat tables.sql

 client_id | value

-----------+-------

(0 rows)

 

select * from origins_tab;

$
at first query we did not use \qecho , it has directly written output and in second query as we used \qecho it has written query

To write the query in the output
Using \set ECHO queries we will be able to write query in output. Below is difference


tech_db=# select * from origins_tab;

 client_id | value

-----------+-------

(0 rows)

 

tech_db=#

tech_db=# \set ECHO queries

tech_db=# select * from origins_tab;

select * from origins_tab;

 client_id | value

-----------+-------

(0 rows)

 

tech_db=#


Feel free to comment and feedback on this. Check out other posts as well.

 

  • Share This:  
  •  Facebook
  •  Twitter
  •  Instagram
  •  Pin
  •  linkedin
  •  reddit
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

3 comments:

  1. Anonymous16 March 2022 at 17:20

    but postgres is not writing errors in output file

    ReplyDelete
    Replies
      Reply
  2. Anonymous5 July 2022 at 03:35

    yes, I observed also that issue that postgres is not writing errors in output files. For so many things I'm really struggeling for hours or days which I solved in Oracle in 5 - 10 minutes...

    ReplyDelete
    Replies
      Reply
  3. Chandan27 March 2023 at 21:27

    Below command will help in writing, query, output and error to outputWithQuery.out file. psql -U postgres --echo-queries < input.sql > outputWithQuery.out 2>&1

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

Author

Atikh Shaikh
View my complete profile

Categories

  • MongoDB (18)
  • Oracle 12c (30)
  • Oracle12cR2 New Feature (3)
  • PostgreSQL (20)
  • RMAN (10)
  • Snowflake (8)
  • mysql (23)
  • oracle (74)

Blog Archive

  • ►  2018 (38)
    • ►  November (25)
    • ►  December (13)
  • ▼  2019 (33)
    • ▼  January (15)
      • Data Types in MongoDB
      • Oracle : RMAN Introduction
      • ORA-00265: instance recovery required, cannot set ...
      • Oracle : Starting with RMAN Commands
      • Oracle RMAN : Incremental Backups
      • MongoDB 4.0 New Features
      • Oracle RMAN : File Section for backup of large dat...
      • ORA-19804, ORA-19809: limit exceeded for recovery ...
      • Oracle RMAN: Fast Incremental Backups
      • The DUAL table in oracle
      • Oracle : Drop Pluggable Database (PDB) in 12c/19c/...
      • Oracle : The oerr Utility (Oracle Error)
      • Oracle RMAN : Incrementally Updated Backups
      • MySQL 8.0 New Features
      • Postgres spooling file and other command line options
    • ►  February (6)
    • ►  March (2)
    • ►  April (5)
    • ►  May (5)
  • ►  2020 (5)
    • ►  April (1)
    • ►  May (2)
    • ►  July (2)
  • ►  2021 (8)
    • ►  June (3)
    • ►  July (3)
    • ►  August (1)
    • ►  December (1)
  • ►  2022 (33)
    • ►  May (3)
    • ►  June (10)
    • ►  July (3)
    • ►  August (4)
    • ►  September (8)
    • ►  October (3)
    • ►  November (2)
  • ►  2023 (14)
    • ►  February (1)
    • ►  April (5)
    • ►  May (2)
    • ►  June (1)
    • ►  September (1)
    • ►  October (1)
    • ►  December (3)
  • ►  2024 (5)
    • ►  January (2)
    • ►  March (3)
  • ►  2025 (6)
    • ►  March (1)
    • ►  April (3)
    • ►  May (2)

Popular Posts

  • ORA-29283: invalid file operation: unexpected "LFI" error (1509)[29437]
    I was trying to export the schema in my windows PC, it got stuck with below error    C:\Users\shaik\Videos\technodba exp>expdp userid...
  • PostgreSQL : How to get data directory location for PostgreSQL instance
    Sometimes, you start working on a PostgreSQL instance but forget about the data directory, here we will discuss different methods to know th...
  • Oracle 23ai : Use of NOVALIDATE Constraints in IMPDP
    While performing impdp operations in the Oracle database, Oracle performs validation checks for every constraint on the imported table, that...
  • ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
    In previous articles, we have learned about user creation and grants  in MySQL in detail, but there are a few privileges called global priv...
  • Oracle Dataguard Broker Configuration (DGMGRL)
    Data Guard Broker is a command-line interface that makes managing primary and standby databases easy. DBA can use a single command to switch...

Labels

oracle Oracle 12c mysql PostgreSQL MongoDB oracle 19c Oracle23c oracle19c Orale PDB-CDB oracle12c python AWS Oracle ASM Virtualbox pluggable database storage engine

Pages

  • Disclaimer
  • Privacy Policy

Follow TechnoDBA

Copyright © Atikh's DBA blog | Powered by Blogger