pg_dump: [archiver (db)] connection to database "mydb" failed: FATAL: database "mydb" does not exist - sql

ok, following these instructions I've runned:
$ PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump
in the same folder where the database is (app_db.sql) but I keep getting:
pg_dump: [archiver (db)] connection to database "mydb" failed: FATAL: database "mydb" does not exist
Why is this happening and what can I do solve it?
Thanks

You have critically misunderstood how PostgreSQL works.
app_db.sql is not a database, it is a database dump. A sequence of text commands that describe the data in the database that can be replayed to create the database.
It isn't like a Microsoft Access .dbx file or a SQLite3 database file, a database stored in a single file that can be manipulated directly. The only way to work with a PostgreSQL database is via a client/server connection to the PostgreSQL server, which stores the actual database contents in a system dependent location like /var/lib/pgsql which you never need to manipulate directly.
To use the database dump, you must restore it into a new empty database using the psql command, eg:
$ createdb mydb
$ psql -f app_db.sql mydb
This will probably fail with permissions errors if you try to run it exactly as written above. You will need to create yourself a user, give that user ownership of mydb, and possibly edit pg_hba.conf to allow yourself to authenticate depending on your system settings.
A more realistic example for a user with unix login name bob might be:
$ sudo -u postgres createuser bob
$ sudo -u postgres createdb -O bob mydb
$ psql -f dpp_db.sql -1 -v ON_ERROR_STOP=1 mydb
I strongly recommend that you read the PostgreSQL tutorial and the user manual.

Related

Trying to load Postgres sample database dvdrental

I'm a newbie and new to Postgresql. I'm following a tutorial online and after downloading and extracting the dvdrental.zip into dvdrental.tar . I changed directory to my postgres bin location. And tried to run
pg_restore -U postgres -d dvdrental '/Users/macbookair/Downloads/dvdrental/dvdrental.tar'
But I got this error:
pg_restore: [archiver (db)] connection to database "dvdrental" failed:
FATAL: role "postgres" does not exist
Meanwhile my dvdrental database has a postgres role.
At first: you must create a database in SQL-Shell:
CREATE DATABASE newdvdrental;
look at first picture
Creating New DataBase
In the next step:
pg_restore --dbname=newdvdrental -U postgres --verbose C:\dvdrental\dvdrental.tar
look at second picture:
Restoring dvdrental.tar to new database
And by checking result in PgAdmin:
checking result

Create and import pgsql database after pg_dump

I am new to postgres. I have exported a large, complex database with the following command in the terminal
pg_dump -U USERNAME DBNAME > dbexport.pgsql
Now that I have transferred this .pgsql file to a different computer, what is the right command to automatically create and restore the exact same database as was exported? Any suggestions would be appreciated
The way you dumped the database, the information about the database itself is not included in the dump (which is a plain SQL file).
You can either use the -C option to include CREATE DATABASE in the dump (the dump has to be restored with psql), or you use the custom format:
pg_dump -F c -U postgres DBNAME -f dbexport.pgsql
That can be restored with pg_restore like this:
pg_restore -C -d postgres -U postgres dbexport.pgsql

Can not import database in postgresql

Hi i am using postgresql . i tried to import a databsee by
$ psql arbles < app.arbles.com.15.08.2014.sql;
ERROR: syntax error at or near ""
LINE 1:
^
it gives me an error ,
I firstly created my database arbles
then i gave one of my database user's full privilages of the database
grant all privileges on database arbles to postgrestest;
3.then i tried the first command and it failed
my app.arbles.com.15.08.2014.sql file is in /var/lib/postgres i also moved it to different locations , but the same error occures.
postgrestest
is also a super user
i also thired with different solutions
1. psql -h hostname -d databasename -U username -f file.sql
2. \i C:/database/db-backup.sql
non of them worked , why is this happening , please help me , thanks in advance
As you indicated that the backup was created using pg_dump, then, most likely, it's not a SQL file. Instead of trying to execute SQL from that file, use pg_restore to restore it:
pg_restore -h hostname -d databasename -U username filename

Error in Mysqldump command

I have a data base named "mig". it has 10 tables. now i want to create a same database in another system so I am using mysqldump command but it shows error.
I entered command as follows :
mysqldump -u root -p root mig >file.sql;
This is the error i got :
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'mysql
dump -u root -p root mig >file.sql' at line 1
I am getting the same error when I use ,
mysqldump -u root -proot mig >file.sql;
How can i fix this ?
Simply try-
mysqldump -u root mig> file.sql
Edit
mysqldump is not a MySQL command, it is a command line utility. You must call it from your shell command line. I hope you are not calling this from MySQL prompt.
When providing password on the command line you should leave no space after -p.
It should look smth like:
mysqldump -u root -proot mig >file.sql;
You can use some tools like MySQL Workbench or SQLyog to import the dump file.
Free version: https://code.google.com/p/sqlyog/wiki/Downloads
When you execute mysqldump from command line, you must have mysql_home/bin directory in your classpath variable or command-line must be pointing to it.
try using
mysqldump -u root -proot mig >(abs_path)/file.sql;
This works for me on my local. Open Terminal and execute the following code (Make sure your are NOT on the MySQL prompt):
mysqldump -uroot -p mig > file.sql
It will ask you to input the password on the next line, for security the password won't be shown.
If you get Access Denied, means the mysql credentials are wrong (or the user you use don't have the right permissions to generate a dump), so make sure you have a valid username and password. I hope it helps.
mysqldump will not run from mysql cli, you will have to run it from windows command prompt:
mysqldump -u username -p database_name > output_file_name.sql;
If you are getting error on running above command 'mysqldump is not recognized as an internal or external command' then navigate to < MySQL Installation Directory/bin/ > and then run the command.
i have the same problem, my situation was i connect from client in local computer to server in SQL instance of Google. Since i read Sahil Mittal said this is comman utilty, i just put in terminal the same command adding -h parameter.
mysqldump -h ip.del.host -u root -p database_name > database_desired_name.sql

Error in creation of postgresql from bash - Install & Import

I am trying to automate the install of debian with postgreSQL but I'm running into issues with my script. The database import of schema.sql into the db1 doesn't seem to be working, and I'm not sure if I even created the database correctly.
This is the code I am using:
# POSTGRES
apt-get install -y postgresql
echo "CREATE ROLE deploy LOGIN ENCRYPTED PASSWORD '$APP_DB_PASS';" | sudo -u postgres psql
su postgres -c "createdb db1 --owner deploy"
su postgres -c "createdb db2 --owner deploy"
service postgresql reload
# IMPORT SQL
psql --username=postgres spider < /etc/schema.sql
When I try to see if the database is created I get the following errors and the SQL import didn't seem to work.
root#li624-168:/etc/app# psql -U root spider
psql: FATAL: role "root" does not exist
root#li624-168:/etc//app# psql -U deploy spider
psql: FATAL: Peer authentication failed for user "deploy"
Can anyone tell me please where I have gone wrong?
Firstly, make sure you check result codes when executing commands. You can abort your bash script by adding set -e at the top. If any single command fails it will stop immediately.
Secondly, take another look at the error message:
Peer authentication failed for user "deploy"
You're trying to login as "deploy" and it seems to recognize the user-name. However, your operating-system user is not called "deploy", so peer auth fails. It looks like you want to login using a password, so set up your pg_hba.conf file to allow that.
Postgres databases are owned by Linux users. So, you need to create an user in postgres tha have the same name of your Linux user. then, you have to use the new user to create your db. Example:
My linux account is razcor
sudo su postgres -c 'createuser -d -E -R -S razcor'
this creates a postgres user
sudo su razcor -c "createdb db1 --owner razcor"
this creates my db
result:
razcor#ubuntu:~$ psql -U razcor db1
psql (8.4.17)
Type "help" for help.
db1=>
In your case create a user named: root
#Richard Huxton: yes, I agree.