how to take backup of only roles/users in postgresql? - postgresql-9.5

I am trying to restore the backup of the database but it's giving me the error of roles, so I got to know that first, we have to take backup of roles/users then we take complete backup but I am unaware of command that can be used. can anyone help me with the command?

You can use pg_dumpall for that with the --globals-only option:
pg_dumpall --globals-only --file=all_roles_and_users.sql -p postgres -h ...
The file all_roles_and_users.sql will contain all roles and role memberships currently defined in the instance (aka "cluster") you connect to.
This nee

Related

How to login to postgresql db - After session kill (for copy database)

I tried to copy a database within the same server using postgresql server
I tried the below query
CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;
And got the below error
ERROR: source database "originaldb" is being accessed by 1 other user
So, I executed the below command
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'originaldb' AND pid <> pg_backend_pid();
Now none of us are able to login/connect back to the database.
When I provide the below command
psql -h 192.xx.xx.x -p 9763 -d originaldb -U postgres
It prompts for a password and upon keying password, it doesn't return any response
May I understand why does this happen? How can I connect to the db back? How do I restart/make the system to let us login back?
Can someone help us with this?
It sounds like something is holding an access exclusive lock on a shared catalog, such as pg_database. If that is the case, no one will be able to log in until that lock gets released. I wouldn't think the session-killing code you ran would cause such a situation, though. Maybe it was just a coincidence.
If you can't find an active session, you can try using system tools to figure out what is going on, like ps -efl|fgrep postgre. Or you can just restart the whole database instance, using whatever method you would usually use to do that, like pg_ctl restart -D <data_directory> or sudo service postgresql restart or some GUI method if you are on an OS that does that.

magento migration from one domain to another

I am transferring my magento site from an old domain to a new domain.
I have exported the database file from the old server and I have done all the necessary changes.
Now I'm trying to run the exported file into the new database but sql is stuck at loading for almost an hour.
Please somebody help.
See loading screen attached. Image here
Thank you.
I would suggest making a backup of the whole 'cPanel' and then reimport it back in. This way you won't mess anything up with the database. If you still do need to perform exporting and reimporting back - make sure you disable the key check by adding this before and after your database.
SET foreign_key_checks = 0;
SET foreign_key_checks = 1;
And to successfully import large database you must increase the memory limit on your 'mysql' within '.ini' file.
I wouldn't do it through a GUI interface. Do you have SSH access? If so, here's how you can run it from Command Line which won't be limited by browser processing.
dump:
mysqldump -u '<<insert user>>' -p --single-transaction --database <<database name>> > data_dump.sql
load:
mysql -p -u '<<insert user>>' <<database name>> < data_dump.sql
It's best to do this as the root user so you don't have any trouble.
On import, if you are getting errors that the definer is not a user, you can either create the definer user or you can run this sed command that will replace the definer in your file with the current mySQL user:
sed -E 's/DEFINER=`[^`]+`#`[^`]+`/DEFINER=<<new user name>>/g' data_dump.sql > cleaned_data_dump.sql
As espradley and damek132 said, combine both the answers. Disable foreign key checks, if it's not. But, mostly it's there while exporting the sql dump.
And use the mysql command line through ssh. You should be up and running in half an hour.

Problem with restoring MySQL database - mysqldump question

I made a backup of my_database like this:
mysqldump --add-drop-database --databases
--user=my_username --password=my_password my_database > backup.sql
Then I deleted the database completely, and tried to restore it like this:
mysql --user=my_username --password=my_password my_database < backup.sql
I got the following error:
ERROR 1049 (42000): Unknown database 'my_database'
What am I doing wrong ?
I need to be able to restore the database if it was changed somehow or removed completely.
Bonus question: is that possible to read the password from a file rather than providing it in the command line ?
If I understand correctly, what you are doing, then you do not need a 'database' argument for the 'mysql' invocation, since the dump should contain the create database statements.
As for the bonus: you want a --defaults-extra-file option
The mysqldump command does not include the create database statement in its output. You need to create the my_database database before running the second command.

OpenERP, data restore problem

I am trying to restore a database backup through the client interface of open ERP. A message appeared "Could not restore DB". I am using Postgresql 8.4.1
Please help!
You can restore your db direct from pgadmin by making blank db and restore your db into.
and other way is by command prompt postgres given below command.
To create --- createdb db_name
To restore ---- psql created db_name < from which db you want to restore
Which version of openerp and postgresql you are using ? even this message appears, please check in postgresql you will find your database restored.
Are you able to create backups on the same server? I've had similar problems on new installations when I haven't created a .pgpass file. The db_user and db_password configuration parameters are used during regular database access, but can't be used for PostgreSQL backup and restore operations. For backup and restore, you need to set up a .pgpass file.

PostgreSQL how to create a copy of a database or schema?

Is there a simple way to create a copy of a database or schema in PostgreSQL 8.1?
I'm testing some software which does a lot of updates to a particular schema within a database, and I'd like to make a copy of it so I can run some comparisons against the original.
If it's on the same server, you just use the CREATE DATABASE command with the TEMPLATE parameter. For example:
CREATE DATABASE newdb WITH TEMPLATE olddb;
pg_dump with the --schema-only option.
If you have to copy the schema from the local database to a remote database, you may use one of the following two options.
Option A
Copy the schema from the local database to a dump file.
pg_dump -U postgres -Cs database > dump_file
Copy the dump file from the local server to the remote server.
scp localuser#localhost:dump_file remoteuser#remotehost:dump_file
Connect to the remote server.
ssh remoteuser#remotehost
Copy the schema from the dump file to the remote database.
psql -U postgres database < dump_file
Option B
Copy the schema directly from the local database to the remote database without using an intermediate file.
pg_dump -h localhost -U postgres -Cs database | psql -h remotehost -U postgres database
This blog post might prove helpful for you if you want to learn more about options for copying the database using pg_dump.
This can be done by running the following command:
CREATE DATABASE [Database to create] WITH TEMPLATE [Database to copy] OWNER [Your username];
Once filled in with your database names and your username, this will create a copy of the specified database. This will work as long as there are no other active connections to the database you wish to copy. If there are other active connections you can temporarily terminate the connections by using this command first:
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '[Database to copy]'
AND pid <> pg_backend_pid();
A good article that I wrote for Chartio's Data School which goes a bit more in depth on how to do this can be found here:
https://dataschool.com/learn/how-to-create-a-copy-of-a-database-in-postgresql-using-psql