when i am trying to create postgres database with bash terminal on windows 11, im using below commands;
createdb 'test'
or
createdb -U postgres 'test'
and nothing happens.
i added the bin folder to paths in "environment variables" of windows. but it didnt solve the problem.
what am i doing wrong?
1st solution:
sudo su - postgres to become postgres
psql -c "create database demo" to create it from shell
2nd solution:
Just simply enter the following commands on bash:
$ createdb -U postgres(db user) dbname
If you set hba_config in pg for the access to the db in network type:
$ createdb -h YOUR_IP -U postgres(db user) dbname
Lastly, if you set password for db user, pg will ask your password to create database.
Note: If nothing works from above, double-check your system environment variables
for me I used Windows command directly, could you first try cd directly to your postgresql bin folder (I suppose createdb application must be there), then try using createdb command. If it works, there must be some wrong config with your Env variable :D (need restart, or just reopen your terminal)
Related
My current system is Ubuntu 18.04
I installed PostgreSQL.
When I run the following command: psql -U node_user dummydb I get the following message: psql: FATAL: Peer authentication failed for user "node_user"
I currently have to access the database using this command: psql -U node_user -h localhost dummydb which works. However, It's quite tedious to enter -h localhost in every time I want to access the database.
The database is on my local machine, and only for learning purposes
Is there a way to run psql -U node_user dummydb without -h localhost and also without entering password?
You can make an alias in your .bashrc or .bash_aliases file. Simply add the following line:
myalias="psql --host= --dbname= --port=5432 --user="
Run source ~/.bashrc
Then you can simply run myalias and type in the password at the prompt, or use .pgpass to save typing in your password
I have a data base named "mig". it has 10 tables. now i want to create a same database in another system so I am using mysqldump command but it shows error.
I entered command as follows :
mysqldump -u root -p root mig >file.sql;
This is the error i got :
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'mysql
dump -u root -p root mig >file.sql' at line 1
I am getting the same error when I use ,
mysqldump -u root -proot mig >file.sql;
How can i fix this ?
Simply try-
mysqldump -u root mig> file.sql
Edit
mysqldump is not a MySQL command, it is a command line utility. You must call it from your shell command line. I hope you are not calling this from MySQL prompt.
When providing password on the command line you should leave no space after -p.
It should look smth like:
mysqldump -u root -proot mig >file.sql;
You can use some tools like MySQL Workbench or SQLyog to import the dump file.
Free version: https://code.google.com/p/sqlyog/wiki/Downloads
When you execute mysqldump from command line, you must have mysql_home/bin directory in your classpath variable or command-line must be pointing to it.
try using
mysqldump -u root -proot mig >(abs_path)/file.sql;
This works for me on my local. Open Terminal and execute the following code (Make sure your are NOT on the MySQL prompt):
mysqldump -uroot -p mig > file.sql
It will ask you to input the password on the next line, for security the password won't be shown.
If you get Access Denied, means the mysql credentials are wrong (or the user you use don't have the right permissions to generate a dump), so make sure you have a valid username and password. I hope it helps.
mysqldump will not run from mysql cli, you will have to run it from windows command prompt:
mysqldump -u username -p database_name > output_file_name.sql;
If you are getting error on running above command 'mysqldump is not recognized as an internal or external command' then navigate to < MySQL Installation Directory/bin/ > and then run the command.
i have the same problem, my situation was i connect from client in local computer to server in SQL instance of Google. Since i read Sahil Mittal said this is comman utilty, i just put in terminal the same command adding -h parameter.
mysqldump -h ip.del.host -u root -p database_name > database_desired_name.sql
I am trying to automate the install of debian with postgreSQL but I'm running into issues with my script. The database import of schema.sql into the db1 doesn't seem to be working, and I'm not sure if I even created the database correctly.
This is the code I am using:
# POSTGRES
apt-get install -y postgresql
echo "CREATE ROLE deploy LOGIN ENCRYPTED PASSWORD '$APP_DB_PASS';" | sudo -u postgres psql
su postgres -c "createdb db1 --owner deploy"
su postgres -c "createdb db2 --owner deploy"
service postgresql reload
# IMPORT SQL
psql --username=postgres spider < /etc/schema.sql
When I try to see if the database is created I get the following errors and the SQL import didn't seem to work.
root#li624-168:/etc/app# psql -U root spider
psql: FATAL: role "root" does not exist
root#li624-168:/etc//app# psql -U deploy spider
psql: FATAL: Peer authentication failed for user "deploy"
Can anyone tell me please where I have gone wrong?
Firstly, make sure you check result codes when executing commands. You can abort your bash script by adding set -e at the top. If any single command fails it will stop immediately.
Secondly, take another look at the error message:
Peer authentication failed for user "deploy"
You're trying to login as "deploy" and it seems to recognize the user-name. However, your operating-system user is not called "deploy", so peer auth fails. It looks like you want to login using a password, so set up your pg_hba.conf file to allow that.
Postgres databases are owned by Linux users. So, you need to create an user in postgres tha have the same name of your Linux user. then, you have to use the new user to create your db. Example:
My linux account is razcor
sudo su postgres -c 'createuser -d -E -R -S razcor'
this creates a postgres user
sudo su razcor -c "createdb db1 --owner razcor"
this creates my db
result:
razcor#ubuntu:~$ psql -U razcor db1
psql (8.4.17)
Type "help" for help.
db1=>
In your case create a user named: root
#Richard Huxton: yes, I agree.
I have a bash shell deployment script (linode stackscript) which runs when I deploy my debian 6.0 server. The script runs as root and the script is as follows:
apt-get update
apt-get install postgresql postgresql-contrib pgadmin3
passwd postgres
su - postgres
psql -c "ALTER USER postgres WITH PASSWORD 'changeme'" -d template1
su - postgres
createdb mytestdb
psql mytestdb
I have two problems:
Firstly, when I run each line manually through shell it works, but when I run it as the stackscript it runs the line passwd postgres but nothing after it.
Secondly, when I run the line passwd postgres it asks me to put in my password manually. Is there any way I can put it in as a variable in the shellscript?
passwd is meant to be used interactively.
The proper command to change a password in a batch is chpasswd.
Example:
#!/bin/sh
echo 'postgres:newpassword' | chpasswd
Also note that the way your script does su - postgres does not look like it's normally done in non-interactive mode.
Better do: su -c 'command1; command2...' - postgres
I have a .sql file that was created by postgresql a while back. I now want to import this file onto a windows machine running postgresql.
How do I do this. The file is about 1.5gb.
You should use psql command line tool:
psql -h hostname -p port_number -U username -f your_file.sql databasename
click on the SQL Shell and log into the database and use import
Server [localhost]:
Database [postgres]:
Port [5432]:
Username [postgres]:
Password for user postgres:
psql (9.2.4)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
postgres=# \i c:/data/data01.sql
start you psql command tool, it will give you dialog like the following
Server [localhost]:
Database [postgres]:
Port [5432]:yourport
Username [postgres]:
Password for user postgres:**********
then connect to your database
postgres=# \c yourdatabase;
then import the file
yourdatabase=# \i c:/path/path/data/data01.sql
note the / for directory separator & no spaces in file path
This also works for me:
psql dbname username < file.sql
command prompt
open your cmd window and type the following (make sure the path of postgres is correct)
."C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -h 127.0.0.1 -p 5432 -U postgres -d dbname <./query.sql
psql -U <dbusername>
if the prompt makes you enter password, do that.
\c <yourdatabasename>
\i 'thepathusing/delimiter.sql'
Two points you need to watch out that
Use / as writing path of the file instead of \.
Use single quote
symbol ' instead of ".
If you're doing it with a URI connection string make sure the arguments are before the URI, Powershell examples:
Works on windows:
.\psql -f TestFile.sql $connString
.\psql -c 'SELECT Version();' $connString
Won't work on windows (URI connection before arguments):
.\psql $connString -c 'SELECT Version();'