Postgresql createdb command hanging when running from groovy script - postgresql

I've tried several different ways to get this to run, all unsuccessfully!
Currently, I have a groovy script that tries to execute the Postgresql (9.2) 'createdb' command like this:
def createDbCmdLine = "-p 5433 --encoding=UTF8 --template=template0 myDatabaseName"
ant.exec(executable:'fullpath/bin/createdb') {
arg(line: "$createDbCmdLine")
env(key:"PGPASSWORD", value:"myPassword")
}
However, this just hangs forever. When I tried creating a string and executing:
["sh", "-c", theStringHere].execute()
the result was the same - hangs forever. In this case though, I printed the string. When I ran that on the command line (directly or via 'sh -c') it worked perfectly - after the command completes, I can enter postgresql via 'psql' do a \l and see the database created.
Anyone know what the problem is?

The most likely issue would be createdb not seeing the environment variable and hanging waiting for the password.
I think you have two options. The first is to continue the shell escapes and try to use a .pgpass file, but the other is to connect to the postgres (or other existing db) and create the database manually. To do this, issue the following SQL:
CREATE DATABASE myDatabaseName WITH TEMPLATE template0 ENCODING utf8;

Related

Ansible playbook change user with shell commands

I am new to Ansible and willing to write a small playbook that switches to another user, gets into Postgres data base, do some changes, exit the data base and switch back to the original user.
This is what I would do manually, but I want to put these commands in the playbook:
sudo su - postgres
psql postgres
DROP DATABASE scm;
CREATE DATABASE scm OWNER scm ENCODING 'UTF8';
\q ##This will quit the database
exit ##This will quit postgres user back to original user
I started writing it into a playbook but it seems to not work:
---
- name: TEST
hosts: master_servers
tasks:
- name: Delete DB
shell:
cmd: psql postgres | DROP DATABASE scm; | CREATE DATABASE scm OWNER scm ENCODING 'UTF8'; | \q | exit
become: yes
become_user: postgres
Here is the error I get:
fatal: [xx.xx.xx.xx]: FAILED! => {
"msg": "Failed to change ownership of the temporary files Ansible needs to create despite connecting as a privileged user. Unprivileged become user would be unable to read the file."
}
Thanks in advance!
I'd suggest that you use postgresql_db module (maintained by the Ansible Core Team).
You can create, delete, or otherwise manipulate PostgreSQL databases in an idempotent way (you should always refrain from using shell and cmd modules whenever possible, since they are not idempotent by design).
There is no problem with your ansible syntax but there is a flaw in shell command you're trying to execute.
| "pipe" connects the stdout of one command to the stdin of another, the way you're executing your command doesn't make any sense.
I would suggest creating a script and copying it over to achieve your goal.

How can I automate running an init script when invoking a postgres DB using psql?

psql (9.6.7, server 9.5.2) on linux
I have an init script... ~/sql.ini that I always want to run after connecting to a PG DB. I can do this by typing "\i ~/sql.ini", but is there a way to do this on the command line (which I alias) ?
Just put your commands in ~/.psqlrc file.
Your alias should call
PSQLRC=~/sql.ini psql
That environment variable specifies the startup file that psql executes automatically.

Automating Database Connection

For a homework, I have a few steps I have to go through every single time I want to connect to the database and it's becoming a really annoying and time-wasting act.
I've already automated part of it. However, my latest attempt at automating the last few commands hasn't been successful.
Initially, I've set up a shortcut to a PuTTy terminal:
Create new Shortcut
Select "C:\Program Files\PuTTY" as the entry point (Start in)
Enter "C:\Program Files\PuTTY\putty.exe" <MY_USERNAME>#arcade.iro.umontreal.ca -pw <MY_PASSWORD> as the Target
Then after double-clicking this shortcut, I entered these two lines (to create and then execute a bash script):
echo "psql -h postgres && \c ift2935 && set search_path to inscriptions_devoir;" > sql.sh
. sql.sh
Eventually, my goal would be to simply be able to write . sql.sh after opening my shortcut to be all set up and ready to go (and actually, maybe even that can be automatized somehow with the shortcut?). However, as it is, my shell script only runs the psql -h postgres command, which successfully launches PostGreSQL.
My question is:
How do I get the two other commands (\c ift2935 and set search_path to inscriptions_devoir;) to automatically run inside PostGreSQL?
EDIT:
Forgot to mention: after the first command of my script executes, I can then type \q to leave PostGreSQL and then the terminal outputs this:
-bash: c: command not found
Which, I think, indicates that the terminal interrupts its current process to actually run PostGreSQL and, on exit, it resumes the script, moving onto the second command, which fails because \c means nothing as a shell command.
While connected to the database, run:
ift2935=> ALTER ROLE <MY_USERNAME> SET search_path TO inscriptions_devoir;
This is your database user. Unless PGUSER is set, this should be the same as your operating system user, but you can always find it with SELECT current_user;.
Then the setting will automatically be active the next time you connect.
In your shell script, change the call to
psql -h postgres -d ift2935
Alternatively, and slightly better in my opinion, is the following, more complicated procedure:
Edit the file .bash_profile in your home directory and add
export PGHOST=postgres
export PGDATABASE=ift2935
Then disconnect and reconnect (this file is executed when you start a login shell).
Instead of running . sql.sh, simply type psql, which is less cumbersome.
Off topic: It is widely held that industriousness is the motor of progress. Nothing could be farther from the truth. Laziness is the mother of invention, specifically laziness paired with curiosity. If you plan to go into the computer engineering business, I promise you a bright future.
I think you should try using the pgpass file.
https://www.postgresql.org/docs/current/libpq-pgpass.html

using pg_dump on remote desktop

I am a novice postgres user employing pgAdminIII on a Windows desktop to connect to a remote postgres db. It connects ok, and everything from within the gui works fine on a very small database. Now I need to make a dump of the whole database (for example called 'mydb') onto my local desktop. I open the command line tool plugin psql.exe and see the prompt
mydb=>
I write this:
mydb=> pg_dump mydb > /users/username/desktop
on pressing Enter, the screen returns
mydb->
( => has become ->) and there it stays for as long as I leave it. No file is written.
I cannot find in documentation the significance of => and -> and would be grateful for assistance.
pg_dump is an executable that is run from the o/s command line, not from within psql.
First: pg_dump is not a SQL statement. It's a program that you run like psql.exe
So to run that locally you need:
pg_dump mydb > c:\users\username\desktop
pg_dump accepts the same connection parameters as psql
The different types of prompts are explained in the manual - although that is somewhat hidden:
https://www.postgresql.org/docs/current/static/app-psql.html#APP-PSQL-PROMPTING
You can enter \set to see the current definition of those three different prompts.

Ending Postgres Query without leaving the command line - Get back to command line automatically

Whenever I run a Postgres query it appears that you have to completely quit out of the command line.
I have seen it done where you can press CTRL-C and you are taken back to the PSQL command line i.e., databasename=>. Additionally, if I am in the middle of viewing results and I press CTRL-C, how can I have Postgres send me back to databasename=>?
Bonus:
Is there a way to script is so if I type usedb databasename folllwed by psql, Postgres will know which database I am referring to and automatically connect me to it so I dont have to type \connect databasename ?
Once a postgres query has run and has returned its table of results in the 'psql' command line environment it should drop you back in to the same database that you ran the previous command from.
If you want to connect directly to a database from your terminal :
psql -d nameofdatabase
If you want to connect using a script you can access postgres by url :
postgres://username:password#localhost/nameofdatabase
where 'localhost' could be replaced by the ip of the database you are trying to connect to if its not on the same machine.
Instead of pressing Ctrl-C, press Ctrl-D. In Unix, Ctrl-D is the End-of-File (EOF) character. That is what will make psql quit---just like if you fed it a script on stdin and it got to the end. It works in many other REPLs too, like irb, rails console, python, R, bash, etc.
The reason Ctrl-C doesn't exit is so that you can use it to abort an individual command, e.g. a long-running query.
EDIT: Also, if you are viewing results and they are paged (they appear on a new screen and you can scroll up and down), you can get back to the psql prompt by typing q. That's because by default the pager used is just less. You can say man less to read more about it. Or experiment with it on any text file: less /etc/services.
Personally I find paging in psql annoying, so I turn it off by creating a file named ~/.psqlrc with this line:
\pset pager
(Also, sorry if you know this already: ~ is just an abbreviation for "my home directory". So ~/.psqlrc is the same as /home/whatever/.psqlrc.)
Bonus: If you want to connect to a specific database, you can say psql -d foo or even just psql foo.
just enter \q or q in the psql terminal