How do I import SQL file from a directory to a postgres database - postgresql

I am trying to import an sql file into my postgress database, this is the process am trying to use
You are now connected to database "test" as user "postgres".
test=# \i 'person.sql'
person.sql: No such file or directory
test=# \i person.sql
person.sql: No such file or directory
test=#
I have a person.sql file in a download folder, and I am trying to use it in the database called test
I have also tried
test=# \i home/camindo/Downloads/person.sql
postgres=#: No such file or directory
its still not working. I am working on linux fedora. Any ideas to make this thing work?

You can run this command on your terminal without logging into PSQL.
psql databasename < person.sql
Or
psql -h hostname -d databasename -U username -f path/to/folder/person.sql
Here hostname is the psql server, like localhost, the username is your Postgres username.
Please ensure you have created a database with the name databasename in PSQL.

Related

Restoring the Dump using PostgreSQL's psql

I'm trying to restore the DB from an SQL file using
psql dbname < dumpfile
but after I connect to my DB psql provides a prompt with the name of the database to which psql is currently connected, followed by the string =>. For example:
testdb=>
So how can I enter the command, or which Meta-Command should I use to restore the DB with the psql command? (Already connected to my DB)
psql is the command line client. You cannot call psql when you are already in an interactive psql session.
Either you connect with psql -d testdb in an interactive session and import the dump with \i dumpfile, or you stay on the shell and call psql in a non-interactive session with the dump file as script: psql -d testdb -f dumpfile
you can use \c <database_name>
it will connect you to the desired database
for example
global=> \l
List of databases
Name | Owner | Encoding
test | pgadmins | UTF8
global=> \c test
You are now connected to database "test" as user "user1".
test=>

Postgres - npm script to drop/add database

I want to simplify my life and automate the process of adding/dropping my test db via an npm script, however I am running into issues.
Attempt 1:
"drop-db:local": "psql postgres \"drop database blog_db; create database blog_db; \\c blog_db; CREATE EXTENSION \"pgcrypto\";\""
After running this, I keep getting the following error
psql: error: could not connect to server: FATAL: Peer authentication failed for user "drop database blog_db; create database blog_db; \c "
Attempt 2:
changed
psql postgres
to
psql -h localhost -U rm postgres
So this opens the db in my terminal but that seems to ignore some stuff as mentioned in the msg below
psql: warning: extra command-line argument "drop database blog_db; create database blog_db; \c blog_db; CREATE EXTENSION pgcrypto;" ignored
What am I doing wrong?
This is a list of my db users
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
rm | Superuser, Create DB | {}
db version: psql (12.2 (Ubuntu 12.2-2.pgdg18.04+1))
You need to use -c or -f with your psql command.
As the psql help shows:
-c, --command=COMMAND run only single command (SQL or internal) and exit
-f, --file=FILENAME execute commands from file, then exit
As you are using multiple commands so it will be better of you use -f followed by a sql file name that has all the commands e.g your drop_create_db.sql file can have following code:
drop database blog_db;
create database blog_db;
\c blog_db;
CREATE EXTENSION "pgcrypto";
And you may run this file by using the following command
"drop-db:local": psql -U postgres -d postgres -p 5432 -f /tmp/drop_create_db.sql

Entering a .sql file into a new postgreSQL database

I am having troubles getting started with psql. I can login using the script below
myusername#ubuntu:~/Desktop/dbscripts$ sudo su - postgres
postgres#ubuntu:~$
But, once here, I cannot figure out how to find my .sql file.
I tried the options supplied by Bolo here:
How to import existing *.sql files in PostgreSQL 8.4?
But they only give
myusername#ubuntu:~/Desktop/dbscripts$ psql -U root -d first -f myscript.sql
psql: FATAL: Peer authentication failed for user "root"
and
myusername#ubuntu:~/Desktop/dbscripts$ psql -f myscript.sqlp
sql: FATAL: role "myusername" does not exist
and
myusername#ubuntu:~/Desktop/dbscripts$ sudo su - postgres
postgres#ubuntu:~$ \i myscript.sql
The program 'i' is currently not installed. To run 'i' please ask your administrator to install the package 'iprint'
This can be handled from any of the above options you tried. In which path do you have myscript.sql? After you do su - postgres, give the full path . So it will be psql -d first -f <pathtosqlfile>/myscript.sql. psql -U root will not work unless you have a user root in the database. Try psql -U postgres. You can do \i sqlscript at psql prompt, not at linux command prompt as you have done. The error you are getting "role "myusername" does not exist" can be avoided either by using -U postgres (or any other db user) or by setting the PGUSER environment variable.

Restoring .dump file - "Permission Denied"

I am setting up a website and am having some trouble restoring a database .dump file. I am using centos7, selinux, postgresql 9.4, and apache2.
This is my pg_hba.conf file.
This is the command I am trying to move the dump:
psql --single-transaction -U postgres db_name < dump_location
When I do this, I get the error:
Permission denied.
Am I missing something or is there someway I should alter my settings? Let me know if you need more information.
Thank you!
The operating system user you are running your shell as does not have permission to read the path dump_location.
Note that this is not necessarily the operating system user you run psql as. In a statement like:
sudo -u postgres psql mydb < /some/path
then /some/path is read as the current user, before sudo, not as user postgres, because it's the shell that performs the input redirection, not psql.
If, in the above example, you wanted to read the file as user postgres you would:
sudo -u postgres psql -f /some/path mydb
That instructs psql to open and read /some/path when it's started.
Just make sure that you are using correct database user and you have at least read permission on the dump file.
"psql -d -U postgres -f "
will work.

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