postgresql create database tutorial - postgresql

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.

Related

Postgres: Run some hard-coded SQLs then drop to interactive terminal

When you start interactive bash, it runs .bashrc first and then it gives you an interactive prompt. Very handy to setup bash in the right way.
I'm trying to do the same with Postgres client (psql). I want to set some session configuration parameters before I run SQL statements interactively. Does psql let me do that?
The -c option and the -f option are the standard ways to run a pre-canned SQL statements, but the man page clearly states that those options are incompatible with the interactive mode.
The analogy to .bashrc extend to the name as well. You want .psqlrc

I am following a PostgreSQL tutorial. Cannot get results from psql --help inside psql shell

I am following a long a video tutorial on PostgreSQL
Within the video the teacher has led me to download PostgreSQL 13. I know have pgAdmin 4 and SQL Shell psql. I've been able to follow the teacher all the way until 33:42. Right before that I was able to follow along in the shell and create a database. Then the video cuts and I notice he does not seem to be in the same psql shell and is now in a shell with zsh at the top.
I'm on a Windows machine and I am a beginner and do not know what shell this is. The instructor then continues to type psql --help. However when I type that same command I am not able to get the same results thus I am not able to continue to follow along. What do I need to do to proceed?
Teacher's shell before video cut:
Teacher's shell after the video cut, shell looks like it says zsh:
The next screen shows a new shell. Just exit your current session and start a new shell.

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

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.