psql.exe postgresql create database via CMD - postgresql

I search to create a database via CMD, i success to create it using this command:
C:\>"C:\Program Files\PostgreSQL\9.3\bin\psql.exe" -U postgres template1
So when i excute this command i can Create my database like this:
template1=# CREATE DATABASE d_base;
My objectif is to create this database with just one line:
like this:
C:\>"C:\Program Files\PostgreSQL\9.3\bin\psql.exe" -U postgres template1 "CREATE DATABASE T;"
But this not work with me, it gave me this error:
i can solve this problem with creating a .bat Script but my objectif is to use just one line,
Is there any solution for that.
Thank you.

You get that error because template1 is not the last argument. You can use -d to specify the database.
Use the database postgres instead of template1.
Try this:
"C:\Program Files\PostgreSQL\9.3\bin\psql.exe" -U postgres -d postgres -c "CREATE DATABASE t"

https://www.postgresql.org/docs/9.3/static/app-psql.html
see --command=command section

Related

Can I drop & create db while executing pg_restore command?

I am trying to execute the following command to restore db in a Docker container:
cmd /c "docker exec -i database-container pg_restore -C -U postgres -d employee -v < C:\\backup\\employee.tar"
But it throws: "pg_restore: error: connection to database "employee" failed: FATAL: database "employee" does not exist" (If I create an empty database with the same name, it is created without any problem).
The reason may be:
When this option is used, the database named with -d is used only to issue the initial DROP DATABASE and CREATE DATABASE commands. All data is restored into the database name that appears in the archive.
So, is it possible to restore db using the same pg_restore command? Or should I modify it by adding drop and create commands to fix the problem?
Finally I concluded to drop and create db before restore. If you have a better solution you are welcome and I will give a try.
docker exec database-container bash -c "dropdb -U postgres employee"
docker exec database-container bash -c "createdb -U postgres employee"
cmd /c "docker exec -i database-container pg_restore -C -U postgres -d employee -v
< C:\\backup\\employee.tar"
You can let pg_restore drop and recreate the database before restoring your backup by providing both parameters --create --clean together.
--create
Create the database before restoring into it. If --clean is also specified, drop and recreate the target database before connecting to it.
See https://www.postgresql.org/docs/12/app-pgrestore.html

PostgreSQL: import/restore database dump (pg_dump version 9.6.5) on Windows

I am a newbie and I have a postgresql large dump of type .sql and i want to import it. I am on Windows 10 and haven't been able to find solution.
I tried to restore using pgAdmin3 but it doesn't show .sql file while restoring. I also found few commands and tried them but nothing seems to work.
I also tried loading the datasource in IntelliJ DataGrid but it doesn't show the correct driver during the loading settings.
Can someone help?
First, figure out if the dump was created with pg_dumpall or pg_dump.
Dumps from pg_dumpall start with:
--
-- PostgreSQL database cluster dump
--
If the dump was created with pg_dump, find out if the -C option was used.
If yes, the dump will contain a line with a CREATE DATABASE statement.
To restore, use psql from the DOS box. I assume that psql is on your PATH.
A dump from pg_dumpall or pg_dump -C is restored with
psql -U postgres -d postgres -f dumpfile.sql
A dump from pg_dump without -C is restored with
psql -U postgres -d mydatabase -f dumpfile.sql
where mydatabase should be replaced with the name of the target database into which the dump should be restored.

postgres: how to execute script inside a .sql file

In SQLite you can do
sqlite3 i.db < x.sql
where x.sql is a create table statement and i.db is the database
What is the equivalent in PostgreSQL ?
The default command line tool for Postgres is psql it supports command line parameters to specify the database and a script name:
psql -d db_name -f x.sql
For details (e.g. how to specify the database user you want to use for the connection) see the manual: https://www.postgresql.org/docs/current/static/app-psql.html
With a similar approach:
psql -U your_role your_db < x.sql
Within psql:
\i /path/to/x.sql

I want to export a script for my database in postgres

I've created a database in pgAdmin, in which I've 1 public schema and 4 custom schema's. These schema contains several functions, sequences and Tables. Now I want to export a script that can create same database with the same structure without any data. Please help me with this....
You are looking for pg_dump with the (default) plain format.
If you can access to the command line, type this
pg_dump --create --clean --schema-only -U your_user -d db_name > file_name.sql
Then in the server you need to create same database run this
psql -U db_user -d db_name < file_name.sql
--clean option tries to drop all objects before creating them.
--create Begin the output with a command to create the database itself and reconnect to the created database.

drop db in postgres

I try dropdb mydbname in shell. It do not give any error. But still when I call \l it is still there.
I logged into the postgres server using sudo -u postgres psql.
Other than my main concern I need to know how to go into the database other than just staying outside of it. (as a example if I want to list the tables)
In order to drop database you can use SQL command (but I do not understand why dropdb didn't work) DROP DATABASE mydbname:
sudo -u postgres psql -c "DROP DATABASE mydbname"
It would be good to check if database is not used:
select * from pg_stat_activity where datname = 'mydbname';
The sudo -u postgres psql connects to postgres database. You need to specify database: sudo -u postgres psql mydbname and then you can use metdata commands like \d, \dt, \dv, ...
When you say "shell" ... do you mean the psql shell, not the unix command line shell?
I'd say you're running into this issue:
Postgresql not creating db with “createdb” as superuser, yet not outputting errors
ie you're trying to use dropdb as a psql command, when it's a unix shell command. You're not getting an error because of this:
In psql, why do some commands have no effect?
You didn't terminate the command with a semicolon.
Are you missing the comma(;)? This command worked for me:
drop database <database_name>;
Server should be running, then:
dropdb <database name>
If server is not running, first try:
pg_ctl start -D <mylocal_db_path> -l <mylogfile.log>