postgresql db take backup and restore with data - postgresql

i would like to take backup and restore of my postgresql database automatically.
take backup automatically from postgresql with cron trigger in windows pc
later i will do restore when ever it required.
i have below bat commands to take back up of my database
F:
cd F:\softwares\postgresql-12.1-3-windows-x64-binaries\pgsql\bin
pg_dump.exe -U postgres -s fuelman > E:\fuel_man_prod_backup\prod.sql
cmd /k
but i am getting
pg_dump: error: too many command-line arguments (first is "-s")
also i need to take both schema structure with data.
Edit :-
removed password -W

"Too many command line" probably is due to -W option; the string "postgres" after -W is interpreted as db name, so following -s gives error. Anyway, when running pg_dump from a script you must not use -W; use .pgpass file or set PGPASSWORD environment variable (look at pg_dump: too many command line arguments for more details).
As for Frank Heikens comment, if you need to dump both object definition and data, avoid -s option. pg_dump documentation is quite clear.

Related

How To Restore Specific Schema From Dump file in PostgreSQL?

I have a dump file (size around 5 GB) which is taken via this command:
pg_dump -U postgres -p 5440 MYPRODDB > MYPRODDB_2022.dmp
The database consists multiple schemas (let's say Schema A,B,C and D) but i need to restore only one schema (schema A).
How can i achieve that? The command below didn't work and gave error:
pg_restore -U postgres -d MYPRODDB -n A -p 5440 < MYPRODDB_2022.dmp
pgrestore: error: input file appears to be a text format dump. please
use psql.
You cannot do that with a plain format dump. That's one of the reasons why you always use a different format unless you need an SQL script.
If you want to stick with a plain text dump:
pg_dump -U postgres -p 5440 -n A MYPRODDB > MYPRODDB_2022.dmp
psql -U postgres -d MYPRODDB -p 5440 -f MYPRODDB_2022.dmp
Though dumping back over the same database as above will throw errors unless you use --clean or its short form -c to create commands to drop existing objects before restoring them:
-c
--clean
Output commands to clean (drop) database objects prior to outputting the commands for creating them. (Unless --if-exists is also specified, restore might generate some harmless error messages, if any objects were not present in the destination database.)
This option is ignored when emitting an archive (non-text) output file. For the archive formats, you can specify the option when you call pg_restore.
Probably also a good idea to throw in --if-exists:
--if-exists
Use conditional commands (i.e., add an IF EXISTS clause) when cleaning database objects. This option is not valid unless --clean is also specified.

PostgreSql Equivalent of sqlplus -S username/password \#lock?

What will be Postgres equivalent of following:
sqlplus -S username/password \#lock.
Also what does #lock means here?
I don't know PostgreSQL, but - as of Oracle, here you are:
That command means that
you'll connect to Oracle's command line tool called SQL*Plus (executable name is sqlplus)
in silent mode (that's what -s does)
providing username and password
and execute .SQL script whose name is lock (I have no idea what it does; you'll have to open it in any text editor and have a look)
Now, how someone establishes connection to PostgreSQL and runs a .SQL script, that's something I wouldn't know, but - reading online documentation - it might be
psql -U username -d database_name -a -f lock
According to the explanations in the comments and the other answer, the equivalent in PostgreSQL should be
psql 'user=username password=password dbname=mydatabase' -f lock

Restoring a postgres database hangs - with no error

I'm currently trying to restore a database from a backup created by pg_dump (which is small ~100mb) from a tar file.
However attempting to restore with
pg_restore -U postgres -f "example.tar"
just keeps hanging and never seems to work.
What's the best way to try to diagnose and fix the issue?
It's waiting for input on stdin.
-f specifies an output file, not an input file. With no input it's waiting for input on stdin. If you type ctrl-d to end input it will say something like pg_restore: error: input file is too short (read 0, expected 5).
The proper invocation is...
pg_restore --dbname=DatabaseName -Upostgres --format=tar example.tar
--dbname is the name of the database to restore to. If it does not exist, add --create to tell pg_restore to create it.
The --format=tar is not strictly necessary, pg_restore can guess the file type, but why risk it?
See the pg_restore docs.

PostgreSQL - read an SQL file into a PostgreSQL database from the commandline

I use Ruby to generate a bunch of SQL commands, and store this into a file.
I then login to my PostgreSQL database. Then I do something like:
\i /tmp/bla.sql
And this populates my database.
This all works fine as it is, no problem here.
I dislike the manual part where I have to use \i, though (because I need this to work in a cron job eventually, and I think commands like \i are only available when you are directly in the interactive psql prompt).
So my question now is:
Is it possible to use a psql command from the command line that directly will start to read in an external file?
You can directly use the psql command as shown below.
Works for me with Ubuntu and Mint. On Windows it should be quite the same...
psql -U user -d database -f filepath
Example:
psql -U postgres -d testdb -f /home/you/file.sql
For more information take a lock at the official documentation: http://www.postgresql.org/docs/current/static/app-psql.html
When you try to execute an sql file using cron, you will also need to set the environment - database name, password etc. This is a short shell script snippet that does it all
source /var/lib/pgsql/scripts/.pgenv
echo $PATH
psql << AAA
select current_date;
select sp_pg_myprocedure(current_date);
AAA
In .pgenv, you set the values such as
export PGPORT=<yourport>
export PGHOST=<yourhost>
export PGDATA=<yourdatadir>
Also have a .pgpass file so that the password is supplied.
http://www.postgresql.org/docs/current/static/libpq-pgpass.html
Replace the part where SELECT is being done with whatever you want to do, or do it as #Kuchi has shown.

pg_dump: too many command line arguments

what is wrong with this command:
pg_dump -U postgres -W admin --disable-triggers -a -t employees -f D:\ddd.txt postgres
This is giving error of too many command-line arguments
Looks like its the -W option. There is no value to go with that option.
-W, --password force password prompt (should happen automatically)
If you want to run the command without typing is a password, use a .pgpass file.
http://www.postgresql.org/docs/9.1/static/libpq-pgpass.html
For posterity, note that pg_dump and pg_restore (and many other commands) cannot process long hyphens that word processors create. If you are cut-pasting command lines from a word processor, be sure it hasn't converted your hyphens to something else in editing. Else you will get command lines that look correct but hopelessly confuse the argument parsers in these tools.
pg_dump and pg_restore need to ask password on commandline, if you put it command, they always give "too many command-line arguments" error. You can use below for setting related environment variable in commandline or batch file:
"SET PGPASSWORD=<password>"
so that you are not asked to enter password manually in your batch file. They use given environment variable.
Instead of passing password with -W flag start with setting temporary variable for postgres:
PGPASSWORD="mypass" pg_dump -U postgres--disable-triggers -a -t employees -f D:\ddd.txt postgres
-W -> will prompt for a password
to take full DB dump
use some thing like
pg_dump -h 192.168.44.200 -p 5432 -U postgres -W -c -C -Fc -f C:\MMM\backup10_3.backup DATABASE_NAME
I got this from copy-pasting, where 1 of the dashes were different.
Was: –-host= (first dash i a "long" dash)
Corrected to --host= solved it
Another option is to add ~/.pgpass file with content like this:
hostname:port:database:username:password
read more here
Additionally, if you don't want password prompt, use connection string directly.
pg_dump 'postgresql://<username>:<password>#localhost:5432/<dbname>'
So, combination with options in original question,
pg_dump 'postgresql://postgres:<password>#localhost:5432/postgres' --table='"employees"' --format='t' --file='D:\ddd.txt' --data-only --disable-triggers
(Don't forget to use quotes when you have letter-casing issues)
reference:
https://www.postgresql.org/docs/current/app-pgdump.html
Postgres dump specific table with a capital letter
2021-11-30, pg v12, windows 10
pg_dump -U postgres -W -F t postgres > C:\myfolder\pg.tar
-U "postgres" as username,
-W to prompt for psd,
-F t means format is .tar,
> C:\myfolder\pg.tar is the destination path and filename