Loading sample database into Postgres - postgresql

first time user.
I purchased a book called SQL for Data Analytics and it comes with a dump file ("data.dump") (link below).
https://github.com/TrainingByPackt/SQL-for-Data-Analytics/tree/master/Datasets
The book advised installing PostgreSQL which I have done and then loading the dump file via command line (see image below).
For some reason, I am able to create the database via command line (it appears in pgAdmin) but nothing happens when I load the dump file.
postgres=# CREATE DATABASE sqlda TEMPLATE template0;
postgres=# psql < /Users/ast12/Documents/data.dump
I have also tried loading the dump file directly via pgAdmin. Again, nothing happens. I'm at a total loss. I bought the book to learn but with my limited knowledge, I can't even set up the environment to do so.
enter image description here]1

If you are already inside the psql prompt, there is no need to call or run psql again.
After you created the database, you need to switch to it, then run the SQL script using the command \i
postgres=# CREATE DATABASE sqlda;
postgres=# \connect sqlda
postgres=# \i /Users/ast12/Documents/data.dump
\connect sqlda changes to the newly created database, and \i loads and runs the SQL script.
For an explanation on why nothing happened for your second command, see In psql, why do some commands have no effect?

Related

How to DROP tables from specific database-schema in Postgresql?

I am new to Postgresql and so far I have not found a way to drop a table from specific database. To give some context:
We are doing a synchronization from Oracle to PostgreSQL of 5 tables. In postgres I have a database SoloCopy and the schema is the default public. In the Postgresql instance we have also 2 more databases SoloSynch and postgres (the default one).
What I want to do is to select SoloCopy database and:
DROP TABLE public.table1;
When I do the above DROP statement table1 is deleted only from the database that was selected when opening SQL Query. But I want to specify the database before that and to be irrelevant from where the SQL Query was open. How can I do that?
I found an answer on my own. Setup can be found here:
Psql in Task Scheduler does not run a file script
Basically I needed to use PSQL and with the connection string there I connect to a specific DB, which I can drop the tables from. The details for PGPASSWORD and the .bat file I ended up creating are in the link above. Had to start everything from CMD and switch to psql from there.

DROP Postgres SCHEMA or TABLE using Windows Batch File

I want to perform DROP and CREATE SCHEMA in PostgreSQL using Batch file. I have PostgresSQL installed on many machines without any interface like PgAdmin.
I am looking for a simple solution to let Technicians perform resetting of database and restore the default backup at production site. Batch file seems to be a good solution.
I already figure out command to restore backup using Batch file. The missing item is the drop/create SCHEMA (As i need to create new SCHEMA before restoring the default back up).
I tried following command to DROP / CREATE SCHEMA. However it only works when used via PgAdmin or if i type manually under postgres=# prompt on CMD.
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO public;
Using the above code in batch file generates following Error.
'drop' is not recognized as an internal or external command, operable program or batch file.
Any suggestions, please let me know. Thanks for help in advance.
Put the statements you want to run into a (text) file e.g. drop.sql
Then in your batch file use:
psql -U <your_username_here> -d <your_database_here> -f drop.sql
psql will prompt you for a password, see Run a PostgreSQL .sql file using command line arguments for possible options to overcome that.

Creating a database dump doesn't seem to be working

I have PostgresQL 9.6.2 installed, and I'm trying to make a backup of one of my databases (my_database) using pg_dump command. It doesn't seem to be working as I see no output file.
I'm using a Mac and from my terminal (and in my project directory) I use:
psql postgres postgres
pg_dump my_database > my_database.bak
being postgres my database default user and password.
I've already tried using sudo psql postgres postgres with the same results.
I imagine that I'm experiencing some kind of lack of permissions but cannot understand it.
So, how to have the right permissions to do this?
Postgres comes with a bunch of stuff, including command-line tools. That includes the standard client, psql, and also the command pg_dump. That needs to be invoked from a shell command line.
If you type it into a psql command line, you'll get a syntax error once you terminate the line (with a ';') -- it's not valid sql, nor a valid command to psql.
Hope that helps!

Postgresql Ubuntu strange behaviour (version incompatibility?)

I'm trying to run a postgresql Database on Ubuntu 16.04 LTS.
First I installed postgres using sudo apt-get install postgresql which installed version 9.5.1. I then created another user and a new database. I've granted all privileges for the new database to new new user and set the owner to the new user also.
I connected to the new database and filled it by restoring an plain backup (dump) I've created from another database (which has postgresql version 9.2) by using \i /path/to/dump.sql. I got no errors, and when I then typen \dt I got the list with the tables.
The problem is: When I now disconnect (\q) and re-connect (same as I connected before, sudo psql -U "username" dbname) and type \dt again, it says "No relations found". When I try to fill it again, I get a bunch of errors like "Relation relationname already exists".
Another problem/symptom appears when I try to use pgAdmin (installed via sudo apt-get install pgadmin3). When trying to connect using localhost, I can't connect using the newly created user (which which I can connect from the terminal). But I can connect using postgres with the password I set via Terminal. I do not see any DB I manually create from command line from pgAdmin tho.
So yeah, my guess is that at least one problem may be version incompatibility between 9.2 and 9.5. Is there any way to test/fix this? But I also think there should be other problems.
What you describe may happen if the SQL dump contains a SET search_path TO...command that sets it to a different value than what your user has by default.
So not only it will create its tables and other objects in that schema, but it will leave that search_path for the rest of the session, so when you do \dt in that same session, it does see and list the newly created tables.
But when you quit and reenter psql, this search_path is not longer in effect, you're back to the default search_path of your user, which supposedly doesn't reach the schema, so \dt no longer "sees" any table.
You can use show search_path to check this setting in a psql session, and grep "SET search_path" in the SQL file to check what it's being set to.
Per comment, it appears to be the case: the dump creates the tables into a schema that is outside of the user's default search path.
A possible solution for that user to work seamlessly would be to update its search path so that it always reaches this schema first. This can be done with:
ALTER USER username SET search_path TO schema1,"$user",public;
where schema1 is what the SQL dump refers to and where it created the tables.

Using psql command line terminal program to update database

Can someone give the steps to updating my database using psql command line terminal program?
I have created a PostgreSQL database in pgAdmin and I have backed it as PLAIN file (plain-text script). I can't restore that file in pgAdmin. In this this website, it says you can execute a plain-text script file using the "psql command line terminal program", to recreate the database and load data.
So I'm just wondering if someone can give steps to doing this so I can update my current database (outside of pgAdmin).
Run in a shell of your database server as user postgres (or any other user with the necessary privileges):
postgres#db:~$psql
CREATE DATABASE mydb;
\c mydb
\i /path/to/backup.sql
Thereby you create a database, connect to it and run the plain text SQL script from the file to restore the contents.
Details about psql in the manual.