Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing to screen in .sql file postgres

This sounds like it should be a very easy thing to do, however, I cannot find ANYWHERE how to do it.

I have a .sql file I am building for an upgrade to my application that alters tables, inserts/updates, etc.

I want to write to the screen after every command finishes.

So, for instance if I have something like:

insert into X... 

I want to see something like,

Starting to insert into table X

Finished inserting into table X

Is this possible in Postgres?

like image 899
El Guapo Avatar asked Jun 29 '13 17:06

El Guapo


People also ask

How do I copy output from PostgreSQL?

The easiest but the most efficient way to export data from a Postgres table to a CSV file is by using the COPY command. COPY command generates a CSV file on the Database Server. You can export the entire table or the results of a query to a CSV file with the COPY TO command.

How do I get SQL script from PostgreSQL?

To generate SQL scripts from your PostgreSQL project click the SQL Script icon on the main toolbar. New modal opens. Click Save Script and select a location where the file should be stored. Option Overwrite existing files allows you to ignore existing scripts and overwrite them without getting a warning.

Can we import .SQL file in PostgreSQL?

To import SQL script file into PostgreSQL database using this tool, take the following steps: Select the database. Navigate to the "SQL" button. Click the "Choose File" (or "Browse") button and select the SQL file from your computer.


2 Answers

If you're just feeding a big pile of SQL to psql then you have a couple options.

You could run psql with --echo-all:

-a
--echo-all
Print all input lines to standard output as they are read. This is more useful for script processing than interactive mode. This is equivalent to setting the variable ECHO to all.

That and the other "echo everything of this type" options (see the manual) are probably too noisy though. If you just want to print things manually, use \echo:

\echo text [ ... ]
Prints the arguments to the standard output, separated by one space and followed by a newline. This can be useful to intersperse information in the output of scripts.

So you can say:

\echo 'Starting to insert into table X' -- big pile of inserts go here... \echo 'Finished inserting into table X' 
like image 60
mu is too short Avatar answered Oct 02 '22 11:10

mu is too short


There's probably a better way to do it. But if you need to use vanilla SQL, try this:

SELECT NULL AS "Starting to insert into table X"; -- big pile of inserts go here... SELECT NULL AS "Finished inserting into table X"; 
like image 43
shanemgrey Avatar answered Oct 02 '22 12:10

shanemgrey