I have a sequence of insert statements which I want to run on a Postgres database. It looks something like this -

INSERT INTO test_admin.test_admins (id,
        VALUES (
                  TO_TIMESTAMP ('10/01/2015 00:00:00.00 -0500',
                                'MM/DD/YYYY fmHH24fm:MI:SS.FF Z'),

Due to the extra spaces preceding each line, neither pgadmin 3, nor the postgres console would accept this query and give syntax errors. What is the best way to remove these?

  • 2
    What is the exact error message you get. The spaces are not a problem. But the you have not closed the ( for the values clause. And you are missing a value for the end_date column – a_horse_with_no_name Aug 22 '16 at 21:52
  • I posted only part of the query for IPR reasons but it is well formed in terms of brackets. By removing spaces, I was able to run one of the inserts. The error says "syntax error near values". – codeninja84 Aug 22 '16 at 21:57
  • Works for me: imgur.com/a/8ryMj You need to post the complete statement, otherwise this is impossible to answer. By removing parts of the statement you probably changed it so that the culprit can no longer be seen. – a_horse_with_no_name Aug 22 '16 at 22:00
  • I've seen similar errors when editors use nonprinting characters in white space. There's no really easy way to clean up. – Craig Ringer Aug 23 '16 at 4:04

As spaces can be freely used in SQL, maybe you have some non-printable characters in your code. This problem usually relates to using Windows and/or word processors for programming. How (using what program) did you write your code?

Always use an editor designed for programming and always save in 'text only' mode or something similar. If you are in Windows, you can use Notepad (until you get a better editor which is out of scope here), copy-paste your code there and save it.


pg-minify can do what you want. In addition, it can compress (option compress) your SQL to its bare minimum, by removing all comments and all optional spaces.

Even better, it can automatically patch multi-line text with the correct E prefix that indicates multi-line syntax. Plus, it does some basic SQL parsing.

Such library as pg-promise, for example, uses it for compressing all SQL scripts on-the-fly for maximum performance.


var minify = require('pg-minify');

var sql = "SELECT 'abc'; -- comments";

minify(sql); //=> SELECT 'abc';
/* or:
minify(sql, {compress: true}); //=> SELECT'abc';

The REPLACE command. I've used it several times for a specific search function. This should do it: REPLACE('your string', ' ', '') It will remove all spaces.

  • 2
    The issue is not about spaces in returned values, it's about spaces in the text of the SQL query/script. You cannot resolve them by including the REPLACE function into your SQL statement, because the problem is running the SQL statement in the first place. – Andriy M Aug 23 '16 at 10:41
  • I see. I must've misread the question. In that case, I don't see why that would be a problem, since the whitespace should be consolidated when it's interpreted. Seems like they missed a bigger issue. – theblackwidower Aug 23 '16 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.