Unable to restore the postgresql data through command prompt - postgresql

I am trying to restore the postgres sql data from a file . I am trying to do so but it is not importing .
Here is the command which i am using:
postgres-# psql -hlocalhost -p5432 -u postgres -d test -f C:/wamp/www/test/database_backups/backup004.sql
Please help me what I am doing wrong .
I am using windows and the above command does not throws any error but it does not import data.
Regards
Surjan

The only immediate thing I can see there is the capitilsation of -u for username (should be -U).
Correction: You're typing the command line into the psql shell.
You should exit to the CMD.EXE shell, and try the command there. With the correct capitalisation of -U, by the way.
OR, use this to replay the script into that psql shell:
\i C:/wamp/www/test/database_backups/backup004.sql
The forward slashes don't cause a problem on my Windows machine.

Related

psql faild to restor file from network drive

i have a dump file on drive z (network drive)
im opening the psql from PgAdmin4
this is the command that im writeing:
psql -U postgres -d postgres -f Z:\DB_BU\md_20220729.sql
and this is the error that im getting:
Invalid command \DB_BU. Try \? for help.
when im doing this:
psql -U postgres -d postgres -f i\ Z:\DB_BU\md_20220729.sql
Invalid command \DB_BU. Try ? for help.
and when im doing this:
psql -U postgres -d postgres -f "Z:\DB_BU\md_20220729.sql"
im not getting any error but also its not restoring the file. how can i restor the file?
You're trying to call psql from within psql or PGAdmin. Since
psql is a standalone program, not an SQL command you can run in PGAdmin SQL window or psql's own, internal meta-command you're getting the error
Invalid command \DB_BU. Try \? for help
indicating that there was an attempt to interpret your entire command as a SQL query or an internal command and that this attempt failed.
You can open "psql tool" from within PGAdmin but your command won't work there either because it's trying to call psql itself, with some command-line options, which you cannot do when you're already inside an interactive psql session. The command
psql -U postgres -d postgres -f Z:\DB_BU\md_20220729.sql
can be used outside psql and PGAdmin, in your terminal, like zsh on Mac, sh/bash on Linux, cmd or PowerShell on Windows, where psql is installed and visible, along with your network path.
If you're able to open the psql tool window in PGAdmin, you can instead try and use an internal psql \i meta-command which is basically the same thing as the -f command-line option, but meant for use inside the psql session:
\i "Z:\DB_BU\md_20220729.sql"

pg_dump windows command prompt invalid command

trying to using pg_dump to backup a postgres db
i connected through the command prompt and here is my command following this tutorial http://www.postgresqltutorial.com/postgresql-backup-database/
pg_dump -U postgres -W -F t lucz_2017 > X:\postgres_backup\lucz_backup.tar
it gives me an error
Invalid command \postgres_backup. Try \? for help.
what am I doing wrong?
the db name and paths are correct
windows 7 running this from the CMD
You are running pg_dump from psql. Get out of psql and run pg_dump command from Windows Command prompt. pg_dump is its own executable, different from psql.
This works for me in Windows PowerShell in Windows 10:
.\pg_dump.exe --username "yourUserName" --no-owner "yourDatabasName" >./filename.sql
To backup my "DVD_RENTAL_DB" database to a local folder on my computer I had to use the below in the Windows command prompt while running it as an administrator:
Don't use shell redirection (>) on Windows with pg_dump. The shell will helpfully "correct" encoding issues and corrupt your dump.
Instead, specify the output filename with the -f option:
"C:\Program Files\PostgreSQL\14\bin\pg_dump" -U postgres -p 5432 -W -F p -h localhost -f C:\Postgres_DB_Backups\DVD_RENTAL_DB.sql DVD_RENTAL_DB
This worked for me ONLY after I put double quotes around the pg_dump executable file path, before when I was adding the file path without double quotes the back up was not working; probably due to spaces in my file path. The PostgreSQL documentation didn't mention anything about double quotes around the pg_dump executable file path.
To Restore my Database I used the following in the Windows command prompt while running it as an administrator:
1. Open the Windows Command Prompt as an Administrator and you will be in this directory:
C:\Windows\System32>
2. Then type the following:
cd C:\Program Files\PostgreSQL\14\bin\
3. Then you'll be here in this directory:
C:\Program Files\PostgreSQL\14\bin>
4. Type the following:
psql -U postgres -d DVD_RENTAL_DB -f C:\Postgres_DB_Backups\DVD_RENTAL_DB.sql
5. You'll be prompted for your password, then your database will be restored.
Steps to using pg_dump on windows
Access cmd as Admin and type
cd path_to_pg_dump PRESS ENTER
pg_dump --username your_user_name
--table=table_name --data-only --column-inserts your_database > my_table_data.sql
PRESS ENTER

COPY command not found in postgres dockerize

I am trying to COPY data from CSV to container
From postgres container shell
$COPY
copy command not found
also tried \copy
but createdb and dropdb works for me.
COPY is not a bash shell command, it's a command run within the psql database query shell.

Running PostgreSQL commands from anywhere on the terminal

First off, let me say that I'm new to both using Mac and PostgreSQL. I just installed Postgres using their installer and it was installed in /Library/Postgres/... when I tried running createdb from the terminal it returned an error createdb: command not found. I ended up using /library/postgresql/9.6/bin/createdb before I coud get it to work.
Here's my question, how do I set it so that I don't have to type in the full path again to use the createdb command.
I'd love a detailed explanation.
Thanks
First you need to execute the psql command to get into the postgresql interacive shell.
In your terminal:
psql
Postgresql interactive shell should start. In this shell
> createdb yourdatabasename;
Btw: If psql is not found you will probably need to add it to your path and restart your terminal, something like this with the path matching your machine:
export PATH=/Library/PostgreSQL/9.5/bin:$PATH

How to import existing *.sql files in PostgreSQL 8.4?

I am using PostgreSQL 8.4, and I have some *.sql files to import into a database. How can I do so?
From the command line:
psql -f 1.sql
psql -f 2.sql
From the psql prompt:
\i 1.sql
\i 2.sql
Note that you may need to import the files in a specific order (for example: data definition before data manipulation). If you've got bash shell (GNU/Linux, Mac OS X, Cygwin) and the files may be imported in the alphabetical order, you may use this command:
for f in *.sql ; do psql -f $f ; done
Here's the documentation of the psql application (thanks, Frank): http://www.postgresql.org/docs/current/static/app-psql.html
in command line first reach the directory where psql is present then write commands like this:
psql [database name] [username]
and then press enter psql asks for password give the user password:
then write
> \i [full path and file name with extension]
then press enter insertion done.
Well, the shortest way I know of, is following:
psql -U {user_name} -d {database_name} -f {file_path} -h {host_name}
database_name: Which database should you insert your file data in.
file_path: Absolute path to the file through which you want to perform the importing.
host_name: The name of the host. For development purposes, it is mostly localhost.
Upon entering this command in console, you will be prompted to enter your password.
Be careful with "/" and "\". Even on Windows the command should be in the form:
\i c:/1.sql
Always preferred using a connection service file (lookup/google 'psql connection service file')
Then simply:
psql service={yourservicename} < {myfile.sql}
Where yourservicename is a section name from the service file.
enter image description here
use following command :-
C:\Program Files\PostgreSQL\12\bin>psql -U username -d databasename -f D:\file.sql