Search code examples
postgresqlpsql

Why do some psql lines have (# or '# after the database name?


I'm using psql in the command line on a Mac. Usually, when I type a command, the opening line reads database_name=#. I terminate the command with a ; and that works nicely.

Sometimes when I write a command (which has been copied and pasted from a textbook I'm working through) I end up with the opening line being one of the following:

postgis_in_action(#

postgis_in_action'#

What do the ( and ' mean please? And how do I escape them to get back to 'normal'?


Solution

  • It means you missing the closing braces or the apostrophe mark.

    Normal one :

    postgres=# select pg_size_pretty(pg_relation_size('test'));                                                                                                                 
    pg_size_pretty
    ----------------
    8192 bytes
    (1 row)
    

    Missed braces/apostrophe:

    postgres=# select pg_size_pretty(pg_relation_size('test')                                                                                                                   
    postgres(# );
    pg_size_pretty
    ----------------
    8192 bytes
    (1 row)
    
    postgres=# select count(*) from test where name = 'vignesh
    postgres'# ';
    count
    -------
    10
    (1 row)
    

    Valid query with no semi-colon will have hyphen:

    postgres=# select count(*) from test where name = 'vignesh'
    postgres-# ;
    count
    -------
    100
    (1 row)
    

    Just cancel that query by Ctrl + C. Since that query is cancelled and not ran, pressing up arrow will not bring that up, as it will not be available in the history.