copy cp using pg_dump + pg_restore, getting error: - postgresql

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.

Related

Restore postgres database with correct ownership in azure (no superuser)

I am currently working on migrating a postgres database between two azure postgres instances. Since these are managed services, I am not able to use a superuser-account (those are not provided). I am first migrating my roles, then my database, with the following commands:
# https://learn.microsoft.com/en-us/azure/postgresql/single-server/how-to-upgrade-using-dump-and-restore#migrate-the-roles
# Migrate the roles
pg_dumpall -r --host=$OLD_HOST --username=$OLD_USERNAME --database=$DB_NAME > roles.sql
sed -i 's/NOSUPERUSER//g' roles.sql
sed -i 's/NOBYPASSRLS//g' roles.sql
psql -f roles.sql --host=$NEW_HOST --username=$NEW_USERNAME --dbname=postgres --clean --if-exists
pg_dump -Z0 -Fd -j 12 -h $OLD_HOST -U $OLD_USERNAME -d $DB_NAME -f $DIRECTORY
pg_restore -h $NEW_HOST -U $NEW_USERNAME -j 12 -Fd -C -d $DB_NAME $DIRECTORY
Unfortunately this results in a couple of errors like these:
pg_restore: from TOC entry 720; 1259 1421377 SEQUENCE id_publicatie_seq svp
pg_restore: error: could not execute query: ERROR: permission denied for schema svp
Command was: ALTER TABLE svp.id_publicatie_seq OWNER TO svp;
pg_restore: from TOC entry 718; 1259 1421365 TABLE publicatie svp
pg_restore: error: could not execute query: ERROR: permission denied for schema svp
Command was: ALTER TABLE svp.publicatie OWNER TO svp;
I'm able to get rid of these errors by adding the --no-owner flag in my pg_restore command, but this is a problem since I specifically want to keep all owners exactly the same as in my original database. Am I overseeing something?

How to restore psql DB using the different ownership

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?

Copying local postgresql to AWS RDS

I have a Postgesql database that I want to copy/replicate from my local machine to an AWS RDS instance.
I've created a dump as follows:
pg_dump -v -U postgres mydb > mydb.dump
I then tried to copy to my RDS as follows:
psql -f mydb.dump -h XXXXXXXXXXXX.us-east-1.rds.amazonaws.com -p 5432 -U postgres -d mydb
However, I get the following error:
psql:mydb.dump:1: error: invalid command \
psql:mydb.dump:6: ERROR: syntax error at or near "ÿ_"
LINE 1: ÿ_-"\o""edrp\nou"tnsctme e
^
I then tried to rule out any issues with RDS by copying the archive to another local database as follows:
pg_restore -U postgres -d mydbcopy mydb.dump
I received an error:
pg_restore: error: input file does not appear to be a valid archive
I also tried the preceding as follows:
psql -f my.dump -U postgres -d mydbcopy
And got the same error as before:
psql:mydb.dump:1: error: invalid command \
psql:mydb.dump:6: ERROR: syntax error at or near "ÿ_"
LINE 1: ÿ_-"\o""edrp\nou"tnsctme e
^
I am using Windows Powershell and Postgresql 13.2.
Am I missing something ?
It seems to be an issue with PSQL tools in view that I'm getting the error even when trying to copy locally.
I managed to solve this problem by using
pg_dump -f dump.sql --inserts mydb
It turns out using the redirect ">" means PowerShell does the file writing and it uses UTF16 which doesn't play nice with psql. The -f flag makes pg_dump do the writing itself.

pg_restore throws warnings and return exit 1

I am trying to restore a PostgreSQL database using the command below:
pg_restore --verbose --clean --no-acl --no-owner -h localhost -p 5432 -U postgres -w -d databaseName Backup.dump
The restoring is done successfully. But it displays below warning message in the end.
WARNING: errors ignored on restore: 97
Due to which my shell script is failing as the command returns exit 1 code.
Can anyone please guide on this?
It can be done by dropping the database, creating it and then storing a clean database.

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.