How to backup and restore Sonarqube database in Postgresql - 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.

Related

How migrate PostgreSQL database from virtual machine to production server?

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".

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

Can't pg_restore DB to RDS: missing pgpass.conf file?

I'm trying to restore my Postgres database to an RDS Postgres Instance in the AWS using pg restore from an EC2 instance.
I am using the following command to restore the database:
pg_restore -v -h host --no-owner -d PostgresDB postgres.dump
Now the problem is that originally, I didn't specify the --no-owner option, and since the owner of the local database that has been backed up, and the owner of the RDS Instance aren't the same. This threw an error, which is why I read that specifying this option helps solve the issue.
However, now I get a
pg_restore: [archiver (db)] connection to database "XY" failed:
FATAL: password authentication failed for user "ec2-user"
error message, although the password is right. Now I read that this does happens with Postgresql, but I can't find a way to solve this on EC2. According to this thread, I need to change the format of one of the configuration files. But this my postgres is an AWS Instance, how can I achieve this? I browsed my EC2 server instance and didn't find any pgpass.conf file (according to 'which' the file doesn't exist on the server). How can I solve this?
What username did you create for Postgres? Surely it isn't ec2-user. You need to be specifying the username and password with the --username and --password options. Here is the documentation.

psql: FATAL: Peer authentication failed for user "expman"

I'm trying to restore a database from backup but I can't connect to postgresql.
namespace :db do
task import: :environment do
import_path = "~/backups"
sql_file = "PostgreSQL.sql"
database_config = Rails.configuration.database_configuration[Rails.env]
system "psql --username=#{database_config['username']} -no-password # {database_config['database']} < #{import_path}/#{sql_file}"
end
end
I tried changing the pg_hba.conf file (peer to md5).
In the console I tried the same thing with the super user postgres, but it still fails.
BTW, does anyone know a better way to restore a database? I used the backup gem.
EDIT:
I restarted the postgresql server and the passed the authentication. But, didn't restored the db. I reverted the changes in the file and just added -h localhost to the psql command. The database restores now. The only errors I get now are:
must be owner of extension plpgsql //and
no privileges could be revoked for "public"
after change pg_hba.conf, you shold reload or send a SIGHUP signal to postmaster pid. so that change applyed.
why not use psql -f to execute the backup sql file?
or you can use pg_dump backup and pg_restore restore. or copy command backup and restore.
LIKE :
digoal=# copy tbl_join_1 to '/home/pg93/tbl_join_1.dmp';
COPY 10
digoal=# delete from tbl_join_1;
DELETE 10
digoal=# copy tbl_join_1 from '/home/pg93/tbl_join_1.dmp';
COPY 10
OR
pg93#db-172-16-3-150-> pg_dump -f ./tbl_join_1.dmp -t tbl_join_1
pg93#db-172-16-3-150-> psql
psql (9.3.3)
Type "help" for help.
digoal=# drop table tbl_join_1;
DROP TABLE
digoal=# \q
pg93#db-172-16-3-150-> psql -f ./tbl_join_1.dmp
SET
SET
SET
SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
ALTER TABLE