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

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

Related

Error in run sql file in postgres | No such file or directory

Problem
when in tried run sql file in psql shell...
give "No such file or directory" error!
$ ls
config.sql config.yaml
$ sudo -i -u postgres psql
postgres=# \i config.sql
config.sql: No such file or directory
thanks for your reply!
Quick solution:
-i => goes to user's home directory!
as result ./config.sql address is incorrect!
just use
$ psql -U <user_name>
postgres=# \i config.sql
man sudo tells you:
-i, --login
Run the shell specified by the target user's password database entry as a login shell. This means that login-specific
resource files such as .profile, .bash_profile or .login will be read by the shell. If a command is specified, it is passed
to the shell for execution via the shell's -c option.
In particular, that will set your current working directory to the home directory of user postgres.
If you want to avoid that, don't use '-i'.

Postgresql Database export to .sql file

I want to export my database as a .sql file.
Can someone help me? The solutions I have found don't work.
A detailed description please.
On Windows 7.
pg_dump defaults to plain SQL export. both data and structure.
open command prompt and
run pg_dump -U username -h localhost databasename >> sqlfile.sql
Above command is preferable as most of the times there will be an error which will be something like - ...FATAL: Peer authentication failed for user ...
In windows, first, make sure the path is added in environment variables PATH
C:\Program Files\PostgreSQL\12\bin
After a successful path adding restart cmd and type command
pg_dump -U username -p portnumber -d dbname -W -f location
this command will export both schema and data
for only schema use -s in place of -W
and for only data use -a.
replace each variable like username, portnumber, dbname and location according to your situation
everything is case sensitive, make sure you insert everything correctly,
and to import
psql -h hostname -p port_number -U username -f your_file.sql databasename
make sure your db is created or creation query is present in .sql file
Documentation: https://www.postgresql.org/docs/current/app-pgdump.html
Go to your command line and run
pg_dump -U userName -h localhost -d databaseName > ~/Desktop/cmsdump.sql

PostgreSQL: Exporting a database on Windows?

I need to export a database I created to get the code for creating the database and inserting rows.
I understand there is a method of using pg_dump, but all the walkthroughs of using it I can find seem to be on Linux.
Can anyone tell me how to do this on Windows?
You have to execute pg_dump located in the bin folder of your PostgreSQL install.
Ex : C:\Program Files\PostgreSQL\9.4\bin.
The command is pg_dump -U *username* -p *port* -d *database* -W -f *filename*
All the parameters are case sensitive ! (Check your username !)
U is for specifying the user that will connect to the DB.
If you don't specify it, pg_dump will use the login you're logged on with.
p for the port. (Default is 5432)
d for the database name
W to force pg_dump to ask for password
f the name of the file where the export should be stored. If you don't specify this, the dump will be displayed in the console.
Example :
pg_dump -U postgres -p 5432 -d postgres -W -f c:\vm\dump.sql
You may need special permissions to export the file to some folders.
(i.e. : C:\program files\ requires administrative rights for writing.)

PostgreSQL - read an SQL file into a PostgreSQL database from the commandline

I use Ruby to generate a bunch of SQL commands, and store this into a file.
I then login to my PostgreSQL database. Then I do something like:
\i /tmp/bla.sql
And this populates my database.
This all works fine as it is, no problem here.
I dislike the manual part where I have to use \i, though (because I need this to work in a cron job eventually, and I think commands like \i are only available when you are directly in the interactive psql prompt).
So my question now is:
Is it possible to use a psql command from the command line that directly will start to read in an external file?
You can directly use the psql command as shown below.
Works for me with Ubuntu and Mint. On Windows it should be quite the same...
psql -U user -d database -f filepath
Example:
psql -U postgres -d testdb -f /home/you/file.sql
For more information take a lock at the official documentation: http://www.postgresql.org/docs/current/static/app-psql.html
When you try to execute an sql file using cron, you will also need to set the environment - database name, password etc. This is a short shell script snippet that does it all
source /var/lib/pgsql/scripts/.pgenv
echo $PATH
psql << AAA
select current_date;
select sp_pg_myprocedure(current_date);
AAA
In .pgenv, you set the values such as
export PGPORT=<yourport>
export PGHOST=<yourhost>
export PGDATA=<yourdatadir>
Also have a .pgpass file so that the password is supplied.
http://www.postgresql.org/docs/current/static/libpq-pgpass.html
Replace the part where SELECT is being done with whatever you want to do, or do it as #Kuchi has shown.

Unable to restore the postgresql data through command prompt

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.