Automating Database Connection - postgresql

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

Related

Difference between psql command and psql interactive terminal

I've tried various combinations of search terms and I can't find an answer to this...
I'm new to Postgres, and I'm enjoying using the psql interactive terminal to run SQL commands. But often when I look things up, I find people using psql as a command rather than as a terminal.
For instance, you can restore a database using this command:
psql database-name < path/to/backup.dmp
My question is, are they the same thing or different things? When I run psql as a standalone command, am I effectively running up an interactive terminal for just that one command? And if so, does that mean that anything which goes after psql will also work as a command typed into the psql terminal? So in the example above, I could also just start up a psql terminal and then run the following command?
postgres=# database-name < path/to/backup.dmp
This is actually basic bash stuff. You should read up on Unix shells to understand that better.
Each process has a standard input, a standard output and a standard error.
By default, the interactive terminal where you started a program will be used for these, so the text you type will be the input of the program, and the output of the program will be shown on your screen.
Bot you can also redirect standard input with
command < file
Then the input for the program will be taken from file rather than from the interactive terminal.
That's one of the ideas in Unix: the user is just another file from which you can read and to which you can write.
So everything before the < is part of the command invocation, and everything after the < is the file to read.
If you want to read and execute an SQL script while in a psql interactive session, use
\i file.sql

postgresql create database tutorial

I am following this tutorial for creating a user in postgresql.
It lists some commands to run, but does not say within what context to run them. I tried running the command psql,which takes me to a prompt like the following...
postgres=#
Then when I enter the commands shown in the tutorial there, absolutely nothing happens at all. What am I missing here?Also, why does the postgress official documentation guide have such gigantic gaping holes in the information it provides?
You appear to have skipped past the part of the documentation that describes the conventions it uses:
SQL commands are preceded by the prompt =>, and shell commands are preceded by the prompt $.
The page you link to says:
$ createdb mydb
So you should run it at the shell prompt and not the postgres prompt. i.e. Don't run the command psql first.
It doesn't tell you to run psql until the next page.

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

How do I execute a query automatically in PostgreSQL when connecting via shell?

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=#