pg_dump without setting search_path - postgresql

When I use pg_dump to export schema from a database, it adds the following line at the beginning:
SELECT pg_catalog.set_config('search_path', '', false);
Is it possible set an option where pg_dump will not add this line? It is causing issues later when I try to execute other SQL commands, without the schema qualifier.
This is the pg_dump command I am using right now:
pg_dump -O -x -h <db-host> -p <db-port> -U <db-user> -d <db-name> --schema public --schema-only > public-schema.sql

No, there is no such option.
I recommend that you restore a dump with psql -f dumpfile rather than using \i to execute it in the current session.

Related

How to get dump only function creation & Stored Process script via command line in PostgreSQL

I am able to get the script for table creation via pg_dump command in postgress sql similar way I need to generate for functions, SP's & views to export into SQL file via pg_dump.
Can you please help on this?
For a Table:
pg_dump -U postgres -d postgres -t clientlocationregions > C:\clientlocationregions.sql
Thanks
Mahesh
This will do:
pg_dump -Fc -s -n <schemaname> -f temp.dump <database name>
pg_restore -l temp.dump | grep FUNCTION > functionalist
pg_restore -L functionalist temp.dump > yourfunctions.sql

psql equivalent for pg_restore for .sql files

I ONLY dump my databases as *.sql files not *.dump files. As a result NONE of the pg_restore commands work. I've been reading through answers and I swear most people have a reading disability lol
I am asking for the equivalent in psql for a common pg_restore commandLine method to restore a database.
I have no intention of dumping my databases as *.dump.
my question is this:
what is the equivalent to:
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d my_db db/latest.dump
using psql
so...
something along the lines of:
psql --verbose --clean --no-acl --no-owner -h localhost -U myuser -d my_db db/latest.sql
With a SQL dump you need to decide whether you want to drop target objects, when dumping the database, not when importing it.
So, you need to use:
pg_dump --clean ....
Then the SQL dump will contain the necessary DROP statements.
Another option is to run drop owned by current_user before doing the import. This however requires that everything is owned by the user doing the import (so you can't run the import as e.g. postgres)
This can be combined with running the SQL dump:
psql -U your_user -d your_db -c 'drop owned by current_user' -f your_dump.sql

Postgres pg_restore command to restore complete schema

am using Postgres EnterpriseDB 9.5.0.5
I have taken a schema dump by using the below command
pg_dump -n 'schema1' db1 > schema1.dump
Now i want to restore it in different database (db2) what is the command i have to use.
i tried
pg_restore -d DB2 schema1.dump;
but it is showing error
pg_restore: [archiver] input file does not appear to be a valid archiver
You have two choices:
if you include no -f option for pg_dump, it creates a sql script so then restore using psql, not pg_restore
You could add -fc to create a custom binary/compressed format and then use pg_restore as you are trying to do.
However pg_restore mostly converts the archive to an SQL script, so it is not useful when you start with an sql script.
pg_dump -Fc mydb > db.dump
pg_restore -c -d mydb db.dump

How to restore postgres database into another database name

I use the postgres today
and got a problem
I dump the database that way
pg_dump zeus_development -U test > zeus_development.dump.out
what if I wnat to restore to another database zeus_production
How could I do?
Simple, first create your database using template0 as your template database:
createdb -U test -T template0 zeus_production
Then, restore your dump on this database:
psql -U test zeus_production -f /path/to/zeus_development.dump.out
When restoring, always use template0 explicit, as it is always an empty and unmodifiable database. If you don't use an explicit template, PostgreSQL will assume template1, and if it has some objects, like a table or function that your dumped database already has, you will get some errors while restoring.
Nonetheless, even if you were restoring on a database with the same name (zeus_development) you should create (or recreate) it the same way. Unless you used -C option while dumping (or -C of pg_restore if using a binary dump), which I don't recommend, because will give you less flexibility (like restoring on a different database name).
The PostgresSQL documentation has influenced me to use the custom format. I've been using it for years and it seems to have various advantages but your mileage may vary. That said, here is what worked for me:
pg_restore --no-owner --dbname postgres --create ~/Desktop/pg_dump
psql --dbname postgres -c 'ALTER DATABASE foodog_production RENAME TO foodog_development'
There was no foodog_development nor foodog_production databases existing before the sequence.
This restores the database from the dump (~/Desktop/pg_dump) which will create it with the name it was dumped as. The rename names the DB to whatever you want.
The --no-owner may not be needed if your user name is the same on both machines. In my case, the dump was done as user1 and the restore done as user2. The new objects need to be owned by user2 and --no-owner achieves this.
Isn't it easier to simply do the following?
createdb -U test -T zeus_development zeus_production
This has an answer on dba.stackexchange, which I reproduce here:
Let's define a few variables to make the rest easier to copy/paste
old_db=my_old_database
new_db=new_database_name
db_dump_file=backups/my_old_database.dump
user=postgres
The following assumes that your backup was created with the "custom" format like this:
pg_dump -U $user -F custom $old_db > "$db_dump_file"
To restore $db_dump_file to a new database name $new_db :
dropdb -U $user --if-exists $new_db
createdb -U $user -T template0 $new_db
pg_restore -U $user -d $new_db "$db_dump_file"
Here's a hacky way of doing it, that only works if you can afford the space and time to use regular .sql format, and if you can safely sed out your database name and user.
$ pg_dump -U my_production_user -h localhost my_production > my_prod_dump.sql
$ sed -i 's/my_production_user/my_staging_user/g' my_prod_dump.sql
$ sed -i 's/my_production/my_staging/g' my_prod_dump.sql
$ mv my_prod_dump.sql my_staging_dump.sql
$ sudo su postgres -c psql
psql> drop database my_staging;
psql> create database my_staging owner my_staging_user;
psql> \c my_staging;
psql> \i my_staging_dump.sql
If your dump does not include the name, the restore will use the DB defined in DESTINATION. Both SOURCE and DESTINATION are Connection URLs.
Dump without --create
pg_dump \
--clean --if-exists \
--file ${dump_path} \
--format=directory \
--jobs 5 \
--no-acl \
--no-owner \
${SOURCE}
Restore without --create
pg_restore \
--clean --if-exists \
--dbname=${DESTINATION} \
--format=directory \
--jobs=5 \
--no-acl \
--no-owner \
$dump_path

Is there a "pg_restore --quiet" option like "psql --quiet"?

psql has a -q / --quiet option (environment variable QUIET). pg_restore does not have a quiet option. Is there any way to make pg_restore not verbosely show the SQL commands that it's executing?
# e.g., here's the verbose output that I don't want to see:
$ pg_restore --cluster 8.4/mycluster mycluster.dump
---- PostgreSQL database dump
--
SET statement_timeout = 0;SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;SET check_function_bodies = false;
...
--
-- Name: data_src; Type: TABLE; Schema: public; Owner: postgres; Tablespace:--
CREATE TABLE data_src (
...
The question seems to imply that pg_restore is executing these SQL commands and you wouldn't want to see them in the output. But outputting them is what it's only supposed to do.
pg_restore has two modes of operation, with or without connecting to a database. When it's called without a database (-d option) as shown in the question:
$ pg_restore --cluster 8.4/mycluster mycluster.dump
then its sole purpose is to output a set of SQL commands in plain text that should be fed to an SQL interpreter to restore the database. Those SQL commands form a coherent set without any concept of verbosity, and they are not executed by pg_restore itself. They're generally redirected into a file for later execution or piped into psql for immediate execution.
You can redirect stdout to a file:
pg_restore --cluster 8.4/mycluster mycluster.dump > pg_restore.log
Or provide the -d option, but what you want is either -f or -d
pg_restore -f pg_restore.sql --cluster 8.4/mycluster mycluster.dump
pg_restore -d yourdatabase --cluster 8.4/mycluster mycluster.dump