How migrate PostgreSQL database from virtual machine to production server? - postgresql

i tried to migrate database from virtual machine to production server (both working on Ubuntu)
but i faced a lot of problems first when i tired to create backup file with that command
pg_dump mydb --file=db.dump --host=localhost --username=admin
this errors show up
pg_dump: [archiver (db)] query failed: ERROR: permission denied for schema topology
pg_dump: [archiver (db)] query was: LOCK TABLE topology.topology IN ACCESS SHARE MODE
then i tried this command and it goes well
pg_dump -Fc mydb > db.dump
and when i tried to restore db to production server i used this command(after creating an user and a database with that user)
psql -d mydb --file=db.dump
this error show up
The input is a PostgreSQL custom-format dump.
Use the pg_restore command-line client to restore this dump to a database.
Then I use this command to restore it
pg_restore -d mydb db.dump
and it go well but when I run the server using this command
python manage.py runserver
This error shows up
return value.replace(b'\0', b'').decode()
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 1-2: invalid continuation byte

Try this as a superuser. Apparently "admin" is not a superuser.
Your first attempt is creating a plain-text backup, which you can restore using "psql". Your second attempt ("-Fc") creates a custom format backup, and you need "pg_restore" to restore it.
And don't forget to migrate the global objects (roles, tablespaces ect), using "pg_dumpall -g".

Related

How to get the PostgreSQL db backup from slave if master down

I have setup PostgreSQL hot stand by replication on Ubuntu. I need to know if master DB server is down, then how to get the backup from the slave.
I have tried this command
pg_dump testdb > /var/lib/postgresql/20190306.bak -p 5433
I got this error:
pg_dump: [archiver (db)] connection to database "channeldb" failed:
FATAL: role "root" does not exist
This specific error has nothing to do with this being a standby server.
Rather, you forgot to use the -U option to specify the database user, so pg_dump assumes it is the same as the operating system user.
Don't use the root user for anything but administrative activities!

export postgres db with pg_dump doesn't work, database "db_name" does not exist

im trying to export PostgreSQL Databases (db name ari_company) using pg_dump command:
C:\Program Files (x86)\PostgreSQL\10\bin>pg_dump --no-owner -U postgres ari_company> dump.sql
and then i got this error:
pg_dump: [archiver (db)] connection to database "ari_company" failed: FATAL: database "ari_company" does not exist
but when i run for default database (postgres) it works and dump.sql is created.
with command psql\l i checked which db exist and there is postgres and my created ari_company. what im doing wrong here ?
problem was that i had two installed server but was connecting to wrong server where there is no named database. databse postgres exits on both server because it's created automatically with name like you set for username

how can I import a database by running just one command?

I try to learn how to import a database into PostgreSQL.
My attempt failed, where I use pg_restore -C ... -d ... to combine creation of a new database and importing from the file to the new database in one command:
$ sudo -u postgres pg_restore -C -d postgres dvdrental.tar
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 2194; 1262 17355 DATABASE pagila postgres
pg_restore: [archiver (db)] could not execute query: ERROR: invalid locale name: "English_United States.1252"
Command was: CREATE DATABASE pagila WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'English_United States.1252' LC_CTYPE = 'English_United States.1252';
pg_restore: [archiver (db)] could not execute query: ERROR: database "pagila" does not exist
Command was: ALTER DATABASE pagila OWNER TO postgres;
pg_restore: [archiver (db)] could not reconnect to database: FATAL: database "pagila" does not exist
My questions are:
I was wondering why my first attempt failed?
Is there a way by running just one command?
The document says:
-C
--create
Create the database before restoring into it. If --clean is also specified, drop and recreate the target database before
connecting to it.
and even provide an example:
Assume we have dumped a database called mydb into a custom-format
dump file:
$ pg_dump -Fc mydb > db.dump
To drop the database and recreate it from the dump:
$ dropdb mydb
$ pg_restore -C -d postgres db.dump
The database named in the -d switch can be any database existing in
the cluster; pg_restore only uses it to issue the CREATE DATABASE
command for mydb . With -C , data is always restored into the
database name that appears in the dump file.
Thanks.
Update:
Thanks again. I found the binary file toc.dat in the dump contains the
CREATE DATABASE pagila WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'English_United States.1252' LC_CTYPE = 'English_United States.1252';`
Is it possible modify it to make sudo -u postgres pg_restore -C -d postgres dvdrental.tar work?
Note that I have a working soluton:
Separate creation of a new database in psql and importing from the file to the new database:
template1=# CREATE DATABASE dvdrental;
CREATE DATABASE
$ sudo -u postgres pg_restore -d dvdrental dvdrental.tar
$
Your import failed because the operating system where you are trying to import the dump does not know the locale English_United States.1252. Either the operating system is not Windows, or the Windows doesn't have that locale installed.
pg_restore -C tries to create the database in the same way as it was on the original system, which is not possible. Explicitly creating the database works because it uses a locale that exists.
There is no way to make that CREATE DATABASE command succeed except to have the locale present, so if that is not possible, you have to run two commands to restore the dump.

pg_restore failing to load dump

Help! All of a sudden, I'm getting this error when trying to restore my local DB from a dump:
$ pg_restore --no-acl --no-owner --dbname my_db tmp/latest.dump
pg_restore: [archiver (db)] Error while INITIALIZING:
pg_restore: [archiver (db)] could not execute query: ERROR: unrecognized configuration parameter "idle_in_transaction_session_timeout"
Command was: SET idle_in_transaction_session_timeout = 0;
Locally, I'm running postgresql 9.6.3. The dump comes from a server that is running 9.4. No known configuration changes to my machine since this command stopped working.
I understand that idle_in_transaction_session_timeout was introduced in 9.6, but I should still be able to load a dump generated by 9.4. So what gives?
The problem is that you used pg_dump from PostgreSQL 9.6 to create the dump of the 9.4 database.
If you want to create a dump that you can load into a 9.4 database, use pg_dump from PostgreSQL 9.4.

How to backup and restore Sonarqube database in Postgresql

I want to rebuild a Sonarqube server with all projects and settings in origin server. The database is Postgresql. But I can't find some documentation for this.
Firstly I tried to backup it in Sonarqube's web UI: Settings -> System -> Backup, then backup it in a xml file. But when I restored it in a new Sonarqube server, all projects in origin server disappeared. It looks that this "backup" is only for settings, not including projects data.
Then I tried to backup database. When building the origin Sonarqube server I've created a database "sonar":
postgres-# create database sonar with owner sonar encoding 'UTF8';
So I tried to dump it:
$ sudo su - postgres
$ pg_dump sonar
pg_dump: [archiver (db)] connection to database "sonar" failed: FATAL: database "sonar" does not exist
Trying to verify its existence:
$ psql -l
But there are only three item: postgres, template0 and template1. Where is the "sonar" database? I also tried to query with user "sonar" but failed:
$ psql -l -U sonar
psql: FATAL: Peer authentication failed for user "sonar"
So how to rebuild the Sonarqube server with all old projects and settings?
Took me a while to see it but the answer is in fact quite obvious:
postgres-# create database sonar with owner sonar encoding 'UTF8';
You didn't create your database. Somehow you are on a continuation line and probably got an error. The first part of the line should read:
postgres=#
When that is not the case, press ctrl-C to return to a new fresh line.