Question

I have sql file(single_table_data.sql) that contains data of only one table(I have taken dump of only one table from server)

Now i have to insert this sql file in to only single table in my database,

so how to import sql file in to single table in postgres ?

Edit

For example i had database name SpeedData and table name CurrentTable, so now i want to insert entire sql file data in to this table CurrentTable

Note: The sql file contains only insert statements(not even create statements)

Was it helpful?

Solution

From the documentation:

psql dbname < infile

This should create a new table with the name of the previously dumped one and insert all data into it. Replace dbname with the name of the new database and infile with the name/path of the file containing the dump, in your case (single_table_data.sql)

If the dump contains only the insert statements, create the table by hand and then execute psql -U your_username -d dbname -f single_table_data.sql

OTHER TIPS

You can simply import sql dump data into your postgres db.

if you have already created DB then don't need to follow 1st step:-

STEP=1

open terminal then run following commands to create postgres database and user:-

sudo -u postgres psql

postgres=# create database mydb;

postgres=# create user myuser with encrypted password 'mypass';

postgres=# grant all privileges on database mydb to myuser;

STEP=2

\c used for selecting your database.

postgres=# \c yourdatabasename

\i used for importing dump data in database.

yourdatabasename=# \i path_of_your_dump_file for example:-

yourdatabasename=# \i /home/developer/projects/django_projects/db_dump.sql

If you face this type of error when you importing data:-

ERROR: role "yourusername" does not exist

so you can make superuser to your "db_user/yourusername" using this command:-

postgres=# ALTER USER fusion WITH SUPERUSER;

ALTER ROLE

If I understood correctly you want to create table from file and fill in with data. Correct command is

PGPASSWORD=<password> psql -f /home/.../filename.sql -h localhost -d database_name -U user_name
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top