Database backup from Postgres - postgresql

I have postgresql installed in my Ubuntu 16.04 system. I am trying to take back up of database but it does not work,
user1#rajeshN ~ $ sudo pg_dump -U postgres teleshop_development1 > pg_backup
[sudo] password for user1:
pg_dump: [archiver (db)] connection to database "teleshop_development1" failed: FATAL: Peer authentication failed for user "postgres"
So I switched to postgres, And tried there but I do not know password
user1#rajeshN ~ $ sudo -i -u postgres
postgres#rajeshN:~$
postgres#rajeshN:~$ sudo pg_dump -U postgres teleshop_development1 > pg_backup
[sudo] password for postgres:
Sorry, try again.
Please suggest how can I get password or back up. Thanks in advance!

When you are already the user "postgres", do not use sudo again. Its just
postgres#rajeshN:~$ pg_dump teleshop_development1 > pg_backup
Or you can do it all in one line
user1#rajeshN ~ $ sudo -u postgres pg_dump teleshop_development1 > pg_backup

Specify PostgreSQL password, EG with -W.
See: https://www.postgresql.org/docs/9.4/app-pgdump.html

Related

executing a .sql file through ubuntu command line for postgres

I have installed postgresql on ubuntu using:
$ sudo apt install postgresql
Now, I have a series of sql queries I would like to fire to create schemas and users and tables etc. I have put those queries in a .sql file as below:
$ sudo nano postgressetup.sql
CREATE SCHEMA schma;
CREATE USER a2i WITH PASSWORD 'password';
GRANT CONNECT ON DATABASE postgres TO schma;
This file has all the queries. I tried something like:
$ psql -U postgres -d postgres -a -f postgressetup.sql
and received error:
psql: FATAL: Peer authentication failed for user "postgres"
I want to know the way I can execute this .sql file.
Note: I've just installed postgres and no further operation is done on it. Any help is appreciated.
You can use the following command explicitly providing db context user
sudo -u postgres psql -U postgres -d postgres -a -f postgressetup.sql

Unable to authenticate in PostgreSQL

I am using PostgreSQL 9.3 in an Ubuntu 14.04 and I always use this procedure to access psql:
sudo -i -u postgres
psql
Until now it returns this:
psql: FATAL: password authentication failed for user "postgres"
password retrieved from file "/var/lib/postgresql/.pgpass"
The content of /var/lib/postgresql/.pgpass is:
*:*:*:postgres:Vdjui6OTF6ab3Jzf77dVeOa88t7OdK
*:*:*:vmail:pxrw23PsaJOBDfZxIcImhAkzUzH4cA
*:*:*:vmailadmin:6XLhFMc5KWYpdXGE7TCfCHZSDkqzXJ
*:*:*:iredapd:5D6Yl2iRu56Dma2uRXDGSAP89JQQHE
*:*:*:iredadmin:xklX0IgJOQVGklDNFlLckPznq5L7ZG
*:*:*:sogo:PZdclHCua2Ip10yddVw4odSjr0dfZk
*:*:*:roundcube:kByuTSkilZWglYzv24unwfenUZkR7u
*:*:*:amavisd:UEBpiDEvsIbogEB5stJl1YP8DvMCWZ
I solved it! I entered this in the terminal instead:
psql -U postgres
Then input my password

PostgreSQL: pg_dump: [archiver (db)] connection to database "dbase" failed: FATAL: Peer authentication failed for user "postgres"

Solved: I added .pgpass in the home.
I have the line:
host all all 127.0.0.1/32 md5
in /etc/postgresql/9.4/main/pg_hba.conf but when I run:
pg_dump -U postgres dbase -f dbase.sql
I get:
pg_dump: [archiver (db)] connection to database "dbase" failed:
FATAL: Peer authentication failed for user "postgres"
The Problem you have is, that if u dont define the Host, your system will decide.
explicit add "-h localhost", this will fix it
If adding -h localhost doesn’t work you can try adding -h 127.0.0.1
pg_dump -h 127.0.0.1 -U <username> -d <database_name> -W > bk_name.sql
I encountered this issue when working on a PostgreSQL database on Ubuntu 20.04.
All the PostgreSQL configuration was all good and they have been working fine.
The issue was that I mistakenly modified the ownership of the PostgreSQL files and all other files on the server to a user called deploy.
So each time I try to run the pg_dump operation it fails with the error:
pg_dump: [archiver (db)] connection to database "dbase" failed:
FATAL: Peer authentication failed for user "postgres"
Here's how I solved it:
I installed and set up another PostgreSQL database server on a new VPS.
Next, I made a backup of the PostgreSQL data directory in the old server:
sudo cp /var/lib/postgresql/12/main /var/lib/postgresql/12/main.bk
And copied the backup into the new server and then replaced PostgreSQL data directory in the new server with it. That is I moved out the PostgreSQL data directory in the new server and copied the PostgreSQL data directory backup of the old one into it. You may need to switch to the PostgreSQL user (sudo -u postgres -i) at some point to perform some operation:
sudo systemctl stop postgresql
sudo mv /home/your-username/main.bk /var/lib/postgresql/12/main
sudo -u postgres -i
sudo mv /var/lib/postgresql/12/main /var/lib/postgresql/12/main.bk2
sudo mv /var/lib/postgresql/12/main.bk /var/lib/postgresql/12/main
Finally, I restarted the PostgreSQL database server in the new server:
sudo systemctl restart postgresql
This time when I tried running the pg_dump operation in the new server, it worked fine.
That's all.
I hope this helps

Creation a Database PostgreSQL 9.3 in Ubuntu

I'm trying to create a database PostgreSQL 9.3 in Ubuntu 14.04 LTS. I've installed the database and now I'm trying to create an user, password and a new database. This is what I've done in the Ubuntu shell:
Installation
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get install postgresql-9.3
Creation of the user "myuser"
sudo -u postgres createuser myuser
Creation of the password
sudo -u postgres psql
postgres=# \password
Enter new password: bla bla...
Enter it again: bla bla...
Creation of the database "newdb"
createdb newdb -O myuser
But I get this error
createdb: could not connect to database template1: FATAL: role "bla bla" does not exist
what can be the issue?
You need to delete and re-create your user as follows :
dropuser myuser
createuser -l myser -P
You will be prompted for the password.
That should do the trick. Hope this helps.
Ok, verify your pg_hba.conf.
First run :
locate pg_hba.conf
This should return the location of the postgresql host based authentifcation configuration file.
Then check that the following line is set accordingly:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
This should enable passwordless access to your DB. You can try with:
psql -U postgres
Try it and let me know if you can connect.

role "postgres" does not exist; cannot createuser

I am on Linux Mint Cinnamon 2.2.16.
During the process of getting Rails up and running, I am having problems with Postgres.
postgres#BL ~ $ psql --version
psql (Postgres-XC) 1.1
(based on PostgreSQL) 9.2.4
I was unable to get anything working under my usual username, so I changed to the default user using
sudo su - postgres
I cannot get anything to work with createuser.
postgres#BL ~ $ psql
psql: FATAL: role "postgres" does not exist
postgres#BL ~ $ createuser -s -U $USER
createuser: could not connect to database postgres: FATAL: role "postgres" does not exist
postgres#BL ~ $ sudo -u postgres createuser newname
Sorry, user postgres is not allowed to execute '/usr/bin/createuser newname' as postgres on BL.
postgres#BL ~ $ which psql
/usr/bin/psql
postgres#BL ~ $ psql \l
psql: FATAL: role "postgres" does not exist
After thoroughly researching the problem and tearing out more than a few hairs, I decided this was some variation of a problem with packaging/installation, similar problem noted here: unable to create user postgres: role "postgres" does not exists
I did a complete uninstall, as per below, and reinstalled without postgresql-xc
How to thoroughly purge and reinstall postgresql on ubuntu?
The new install had expected behavior with the user "postgres" and I was able to add myself as a superuser and create new databases. After some post-installation finagling, Rails seems to be running and playing nice with postgres.
You have to authenticate to psql as the superuser in order to manage users.
For example
psql -U root
Afterwards
create user paige with password 'paige';