How to restore psql DB using the different ownership - postgresql

I am taking pg_dump with --no-owner permission and trying to restore it in another deb.
I took pg_dump using below command and restore it in another db using the psql command.
pg_dump dbname > dname.sql
psql -d dbname -f filename
When I try to access the db I got
"ERROR: must be owner of function *** (SQLSTATE 42501) " .
Then I noticed the DB table ownership is postgres instead of myuser. I tried to restore it using pg_restore as I used .sql format it is not allowing. Then I tried with psql -U myuser dbname -f filename. I got below error message, though the user id is available.
psql: error: FATAL: Peer authentication failed for user "****"
How do I correct it while taking the dump or while restoring how to include the user1 permission?

Related

copy cp using pg_dump + pg_restore, getting error:

Here are my commands:
db_conn_str="${db_prefix}#${db_host}/${db_name}"
pg_dump -w --schema-only --clean --create \
-U dev -d "$db_conn_str" > "$tmp_folder/dev_dump.sql"
psql -h localhost -U postgres -1 -f "$tmp_folder/dev_dump.sql"
I am getting this error:
pg_dump: error: query failed: ERROR: permission denied for table
cp_users
pg_dump: error: query was: LOCK TABLE public.cp_users IN
ACCESS SHARE MODE
does anyone know how to mitigate that error? My goal is to copy a database from a remote db to my local db.
You need to give user dev the right to SELECT from all tables or use a different user.

Unable to restore psql database from pg_dump with a different username

I need to dump a postgres database from computer1 with postgres username1 and then restore it on computer2 with postgres username2. I keep running into the error that looks like the backup file wants to use username1:
When I run this on computer2:
psql dbname < backupname.pgsql
I get this error:
ERROR: role "username1" does not exist
I have tried:
// Dumping from computer1:
pg_dump dbname > backupname.sql
pg_dump dbname > backupname.pgsql
pg_dump -U username1 dbname -N topology -T spacial_ref_sys > backupname.pgsql
// Restoring on computer2:
psql dbname < backupname.pgsql
Is it the dumping or the restoring that needs to be modified to get past this?
The problem is with the dumping. With insight from this post I was able to resolve this using:
// On Computer1
pg_dump dbname -O -x > backupname.sql
// On Computer2
psql dbname < backupname.sql
The option flags used with pg_dump are:
-O <-- No owner
Do not output commands to set ownership of objects to match the original database
-x <-- No privileges
Prevent dumping of access privileges (grant/revoke commands)
See the PostgreSQL docs for pg_dump for more info on the option flags.
You don't need to cripple your dump by discarding owner/privileges. You can do it at restore time.
Use pg_restore with the --no-acl (and probably --no-owner) options:
-x
--no-privileges
--no-acl
Prevent restoration of access privileges (grant/revoke commands).
--no-owner
Do not output commands to set ownership of objects to match the
original database. By default, pg_restore issues ALTER OWNER or SET
SESSION AUTHORIZATION statements to set ownership of created schema
elements. These statements will fail unless the initial connection
to the database is made by a superuser (or the same user that owns
all of the objects in the script). With -O, any user name can be
used for the initial connection, and this user will own all the
created objects.
So something like:
pg_restore --no-privileges --no-owner -U postgres --clean ... $Your_sql_backup
If you are using pgAdmin then you can either remove the checkbox in DumpOptions #2 with Owner and otherwise remove the privilege like
--no-privileges and remove ownership like --no-password in the dump query like
/usr/bin/pg_dump --host localhost --port 5432 --username "postgres" --no-password --format custom --no-privileges --no-tablespaces --verbose --file "as" "databasename".
Also if you have constraints on the table then disable triggers also while creating the dump.
If you cannot create another backup of the database then the alternate way is to replicate the owner and roles of the dumped database to the new database. If you don't do that then you will get an error saying
'ACL does not exist' (not sure as faced it long back)

How to restore postgres db from gzip file using psql? (arelle: XBRL SEC DB)

I downloaded the xbrldb_SEC_pg_2014-11-02.pg.gzip postgres pg_dump file from arelle.org. I then ran the schema ddl file in pgAdminIII and it recreated all of the databases, functions, etc.
When I try to restore the databases using the following:
desktop:~/Downloads$ sudo postgres zcat xbrldb_SEC_pg_2014-11-02.pg.gzip | psql -U postgres public
I get:
sudo: postgres: command not found psql: FATAL: Peer authentication failed for user "postgres"
I can zcat the file into a file to expand it. Looks like it is a pg_dump file.
postgres=> pg_restore -a /home/jeremy/Downloads/xbrldb_SEC_pg_2014-11-02.txt
postgres-> ;
ERROR: syntax error at or near "pg_restore"
LINE 1: pg_restore -a /home/jeremy/Downloads/xbrldb_SEC_pg_2014-11-0...
^
postgres=> pg_restore -a postgres /home/jeremy/Downloads/xbrldb_SEC_pg_2014-11-02.txt;
ERROR: syntax error at or near "pg_restore"
LINE 1: pg_restore -a postgres /home/jeremy/Downloads/xbrldb_SEC_pg_...
So then I tried to use PG Admin III, and my output:
/usr/bin/pg_restore --host localhost --port 5432 --username "postgres" --dbname "public" --role "postgres" --no-password --section data --data-only --exit-on-error --table accession --schema public --verbose "/home/jeremy/Downloads/xbrldb_SEC_pg_2014-11-02.backup"
pg_restore: [archiver] input file appears to be a text format dump. Please use psql.
Process returned exit code 1.
May I please ask what I need to do to get the databases restored?
Does anyone know what I need to do to get the database updated from 2014-11-02 to the current date?
You should run psql as postgres user, not zcat, so try to use following:
zcat xbrldb_SEC_pg_2014-11-02.pg.gzip | sudo -u postgres psql public
PS pg_restore is an utility, not a PostgreSQL command, that means you should run it from command line, not from psql.

Entering a .sql file into a new postgreSQL database

I am having troubles getting started with psql. I can login using the script below
myusername#ubuntu:~/Desktop/dbscripts$ sudo su - postgres
postgres#ubuntu:~$
But, once here, I cannot figure out how to find my .sql file.
I tried the options supplied by Bolo here:
How to import existing *.sql files in PostgreSQL 8.4?
But they only give
myusername#ubuntu:~/Desktop/dbscripts$ psql -U root -d first -f myscript.sql
psql: FATAL: Peer authentication failed for user "root"
and
myusername#ubuntu:~/Desktop/dbscripts$ psql -f myscript.sqlp
sql: FATAL: role "myusername" does not exist
and
myusername#ubuntu:~/Desktop/dbscripts$ sudo su - postgres
postgres#ubuntu:~$ \i myscript.sql
The program 'i' is currently not installed. To run 'i' please ask your administrator to install the package 'iprint'
This can be handled from any of the above options you tried. In which path do you have myscript.sql? After you do su - postgres, give the full path . So it will be psql -d first -f <pathtosqlfile>/myscript.sql. psql -U root will not work unless you have a user root in the database. Try psql -U postgres. You can do \i sqlscript at psql prompt, not at linux command prompt as you have done. The error you are getting "role "myusername" does not exist" can be avoided either by using -U postgres (or any other db user) or by setting the PGUSER environment variable.

Unable To Run PSQL

Whenever I try to run 'psql' I receieve the following error message:
ahcarpenter#ubuntu:~$ psql
psql: FATAL: role "ahcarpenter" does not exist
Any ideas on how to fix this?
That happens because
$ psql
is equivalent to
$ psql ahcarpenter -U ahcarpenter
As that user does not exist enter as user postgres
$ psql -U postgres
Once inside create the user and the database "ahcarpenter".
create user ahcarpenter;
create database ahcarpenter with owner ahcarpenter;
exit
\q
Reenter
$ psql