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.
Related
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
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.
I have installed Postgresql on my Windows 10 PC. I have used the pgAdmin II tool to create a database called company, and now I want to start the database server running. I cannot figure out how to do this.
I have run the start command on the postgres command line, and nothing seems to happen.
What I doing is:
postgres=# pg_ctl start
postgres=# pg_ctl status
postgres=# pg_ctl restart
postgres=# pg_ctl start company
postgres=# pg_ctl status
.....-> I am seeing nothing returned.
Go inside bin folder in C drive where Postgres is installed.
run following command in git bash or Command prompt:
pg_ctl.exe restart -D "<path upto data>"
Ex:
pg_ctl.exe restart -D "C:\Program Files\PostgreSQL\9.6\data"
Another way:
type "services.msc" in run popup(windows + R).
This will show all services running
Select Postgres service from list and click on start/stop/restart.
Thanks
pg_ctl is a command line (Windows) program not a SQL statement. You need to do that from a cmd.exe. Or use net start postgresql-9.5
If you have installed Postgres through the installer, you should start the Windows service instead of running pg_ctl manually, e.g. using:
net start postgresql-9.5
Note that the name of the service might be different in your installation. Another option is to start the service through the Windows control panel
I have used the pgAdmin II tool to create a database called company
Which means that Postgres is already running, so I don't understand why you think you need to do that again. Especially because the installer typically sets the service to start automatically when Windows is started.
The reason you are not seeing any result is that psql requires every SQL command to be terminated with ; in your case it's simply waiting for you to finish the statement.
See here for more details: In psql, why do some commands have no effect?
If you have installed postgres via the Windows installer you can start it in Services like so:
After a lot of search and tests i found the solution :
if you are in windows :
1 - first you must found the PG databases directory
execute the command as sql command in pgAdmin query tools
$ show data_directory;
result :
------------------------
- D:/PG_DATA/data -
------------------------
2 - go to the bin directory of postgres in my case it's located "c:/programms/postgresSql/bin"
and open a command prompt (CMD) and execute this command :
pg_ctl -D "D:\PSG_SQL\data" restart
This should do it.
The simplest way to start/stop/restart the installed PostgreSQL Server on your Windows device is as follows:
Start -> net start postgresql-x64-14
Stop -> net stop postgresql-x64-14
Restart -> net stop postgresql-x64-14 && net start postgresql-x64-14
The version number must be changed to take into account the installed version of your PostgreSQL Server.
For windows the following command worked well for me
pg_ctl.exe restart -D "<path_to_data>"
Eg: pg_ctl.exe restart -D "D:\Program Files\PostgreSQL\13\data"
If you are getting an error "psql.exe' is not recognized as an internal or external command,... "
There can be :
Causes
System is unable to find the psql.exe tool, because the path to this tool is not specified in the system environment variable PATH
or
- PostgreSQL Database client not installed on your PC
Since you have already installed PostgreSQL the latter can not be the issue(assuming everything is installed as expected)
In order to fix the first one "please specify the full path to the bin directory in the PostgreSQL installation folder, where this tool resides."
For example
Path: "C:\Program Files\PostgreSQL\10\bin"
I found using
net start postgres_service_name
the only reliable way to operate Postgres on Windows
first find your binaries file where it is saved.
get the path in terminal mine is
C:\Users\LENOVO\Documents\postgresql-9.5.21-1-windows-x64-binaries
(1)\pgsql\bin
then find your local user data path, it is in mostly
C:\usr\local\pgsql\data
now all we have to hit following command in the binary terminal path:
C:\Users\LENOVO\Documents\postgresql-9.5.21-1-windows-x64-binaries (1)\pgsql\bin>pg_ctl -D "C:\usr\local\pgsql\data" start
all done!
autovaccum launcher started! cheers!
Remove Postmaster file in "C:\Program Files\PostgreSQL\9.6\data"
and restart the PostgreSQL services
There are different way to open PostgreSql database .
1> One of them is by going windows and select pgAdmin4 or pgAdmin3 depends to version you use and entering password you can access you database .
2> Another one is by terminal :
To able to select from terminal you have to add the path of your installed postgresql by going enviroment variables . To do that got to installed postgresql file and select the path of bin and add to enviroment variable of window setting .
after that you can type in terminal : psql -U postgres -h localhost
Hit enter and it ask you password . After giving password you can create database and tables and can access it .
I was try to solve the problem with Windows Terminal and I've cannot to solve it. Use Windows R + cmd (if you are using Windows) for it work!
The easiest way to enable pg_ctl command is to go to your PostgreSQL directory ~\PostgreSQL\version\bin\ and execute the pg_ctl.exe. Afterwards the pg_ctl commands will be available.
I want to be able to execute a statement automatically when I connect to Postgres with psql and then remain connected so I can type in further commands.
Currently, every time I connect, the first thing I do is type:
SET search_path = 'something';
Or maybe I would want to do something else like:
SELECT COUNT(*) FROM sometable;
I know there is a -c argument to psql that will execute a command and then exit. I'm looking for a way I can execute a command upon connecting and then remain in the client.
(Note: I prefer not to alter the database, schema or role to make the search_path permanent, as all of the solutions I have found seem to dictate. I want to set it every time I connect. But again, this question could apply any SQL statement.)
I have tried this:
echo "SET search_path TO 'mything'" | psql
but that behaves the same way as:
psql -c "SET search_path TO 'mything'"
Is what I'm asking for doable?
psql will look for, and execute any commands found in, a couple of places every time it starts up. One is the system-wide psqlrc file, and one is in the home-directory of the login that's running psql, ~/.psqlrc.
So, you could add the SET command that you always want to be run, to your .psqlrc file, and it'll get executed every time you start up. See the example below:
~ $ cat ~/.psqlrc
SET search_path='mything';
~ $ psql
SET search_path='mything';
SET
psql (8.4.20, server 9.2.10)
WARNING: psql version 8.4, server version 9.2.
Some psql features might not work.
Type "help" for help.
rhnschema=# show search_path;
search_path
-------------
mything
(1 row)
rhnschema=#
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;