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.
Related
I have a .sql file of the database backup. PostgreSQL is installed in my system and I use pgadmin4 to communicate with database. I want to restore this .sql file using pgadmin. Can someone please guide me.
An SQL script that was generated with pg_dump without the --inserts option cannot be restored with pgAdmin, because the COPY data are mixed with the statements. You need psql for that.
You could use one of the other backup formats ("custom", "directory" or "tar") and use pgAdmin to restore that. But all that pgAdmin does in this case is call pg_restore, so you might as well do that yourself from the command line.
Another option is to launch the "psql tool" from pgAdmin, which is nothing else than psql. Then you can load the dump with \i dumpfile.sql.
I'm having difficulty restoring a DB to an AWS RDS Postgresql instance. Context is that i am backing up from one RDS instance and restoring to another RDS insurance. They both have the same version of Postgresql 9.6.5.
I was able to take a dump using the following command:
./pg_dump.exe -U dbuser -W -h prod-pgsql-rds.3ft5coqxjdnq.eu-west-2.rds.amazonaws.com -d devdb > c:\tmp\backup.sql
From the resulting .sql file, I then attempted a restore to another RDS instance which is also using Postgresql 9.6.5 using below command:
./pg_restore.exe -U dbuser -d testdevdb -h dev-pgsql-rds.cym8coqx52lq.eu-west-2.rds.amazonaws.com "c:\tmp\backup.sql"
*I also tried the -f switch in the above restore command instead of the " " quotes before/after the file name
But when I try to restore it to a newly created database I get the following error:
pg_restore: [archiver] input file does not appear to be a valid archive
Can anyone help? FYI, I am using PGAdmin 4 via Windows PowerShell. I have to edit some of the values in the strings above due to data sensitivity.
pg_restore is only used for the other, non-plain-text output formats that pg_dump can output. For .sql dumps, you just use psql. See the docs on restoring from backups.
In a Unix env, you'd do psql [yourflags] < /tmp/backup.sql, but I'm unfamiliar with powershell and don't know if it supports < for input redirection; hopefully either it's present or you know the equivalent PowerShell syntax.
So I couldn't get psql or pg_restore to work so opted to import the .SQL file into via the SQL query tool in PGAmdin. This through up some errors so had to make several changes to the .SQL file and perform below:
Commented out a couple of lines that were causing errors
Elevated permissions for the user and made him the owner of for the Schema and DB properties by right-clicking on these via PGAdmin
The .sql file was making several references to the user from the source RDS DB so had to do a find and replace with a user account created for the destination RDS DB. Alternatively, I could have just created a new user on the destination DB with the same username and password as the source DB and then make him the owner in ref to step 2.
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?
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!
While using psql, I want to change the initial database connection.
I had a database named "test" as the initial connection.
When running psql from the command line my prompt would be test=#
After deleting the "test" database, and running psql in command line, I get the following error:
psql: FATAL: database "test" does not exist
I understand what this means, but how do I go about setting the "postgres" database as the default?
Instead of typing psql postgres each time.
You can configure the default behavior of psql -- and in fact any program using the libpq client library -- through environment variables.
In your ~/.bashrc or similar:
export PGDATABASE=postgres
The PostgreSQL documentation contains a complete list.