How do you add a pre-existing database to Postgres? - sql

I am using Postgres in my production server that houses my Django application. I am trying to create a database in psql that already exists. I have the host name, username, password, port, all that good stuff, but after reading Postgres' documentation it seems that there is no way to add in a prexisting database.

No, you cannot create a database if it's already created.
It seems like you have a dump (a .sql file) that you want to load in your production server.
By default pg_dump will not include the CREATE DATABASE statement into the .sql file so you should be able to load the .sql file just doing:
psql -h <DB_HOST> -U <DB_USER> -p <DB_PORT> -W <DB_NAME> < <PATH_TO_YOUR_SQL_FILE>
Example:
psql -h localhost -U db_user -p 5432 -W db_name < /tmp/my_dump.sql
That's it.
Let me know if you have any issues doing that.
Cheers

Related

Using mysqldump when host is set to characters not numbers

I am trying to use mysqldump, but I am having an error with it, my host isn't set to an ip, but instead it is set up to be something different, it is set up to be like db1, db2, etc...
Some things I have tried, but haven't worked.
$ mysqldump --host=db1 -u User -pPassword dbname > db.sql
$ mysqldump -h db1 -u User -pPassword dbname > db.sql
I can connect perfectly whenever I use my db1 as host, but I am just having problem using mysqldump.
Thank you all so much for your help and ideas on how to fix this.

mysqldump ask for password even thou given on commandline/config-file

I am in the process of migrating my MySQL installation to Amazon RDS and they run MySQL Server version 5.6.12.
I got the client tools of version 5.6.13 and trying to use mysqldump for automated backups.
I always get the question to enter password which block my scripting of backups.
I looks like this:
ubuntu#ip-10-48-203-112:~$ mysqldump --user=dbadmin -pmysecretpassword -h someserver.eu-west-1.rds.amazonaws.com -p skygd > dump.sql
Warning: Using a password on the command line interface can be insecure.
Enter password:
I have tried with a configuration file .my.cnf
[client]
user=dbadmin
password=mysecretpassword
And it is picked up ok, if I run:
mysqldump would have been started with the following arguments: --port=3306 --socket=/var/run/mysqld/mysqld.sock --quick --quote-names --max_allowed_packet=16M --user=dbadmin --password=mysecretpassword
But still same question about enter password.
Are there a bug in 5.6.13 that doesn't allow automated login with password?
mysqldump --user=dbadmin --password=mysecretpassword -h someserver.eu-west-1.rds.amazonaws.com skygd > dump.sql
you typed -p at the end of the line
It is a better option to mention mysql password at the end of the first command.
mysqldump -uUsername -p"space-here"Databasename -h"space-here" Hostname >xyz.sql
And for database import use
mysql -uUsername -p"space-here"Databasename -h"space-here"Hostname

MySQLDump to local machine from remote server connected via SSH

mysqldump -h xxx.xxx.xxx.xxx -u username -ppassword databasename > C:\path\to\store\file
It seemed to work as it paused while the file was downloading, however no file appears once it completes.
Do I have something wrong in the command line?
Use like this:
mysqldump -P3306 -h192.168.20.151 -u root -p database > c:/my.sql
Hope to help you:)
Edition for linux
mysqldump -u root -p databasename > ~/Downlaods/filename.sql
Simply run mysqldump -h xxx.xxx.xxx.xxx -u username -ppassword databasename > C:\path\to\store\file from the command prompt on your local machine.
I don't understand why you involve ssh in your question but...
First try the same command without redirecting it to a file to see that you can connect to the database.
Second make sure that you can write to that location (try to create and edit a file in the same path).
If those to work your command should work.

Add database name to sql file when exporting using mysqldump via linux

I need to backup a range of databases each day and I would like to do this via command line.
I'm using mysqldump to dump the db into a folder on the root of the server appended with the date. I would like to add the name of the database dynamically to the exported filename, rather than hard coding it into the query. Currently I have:
[~]# mysqldump -u user -h localhost -p unique_database_name > unique_database_name_1_$(date +%d%m%y).sql
The goal is to have 'unique_database_name' appended to the filename, so the script is a little more portable.
This script would do it:
#!/bin/bash
dbs='firstdb seconddb thirddb'
echo -n 'Enter database password: '
read pw
for db in $dbs
do
mysqldump -u user -h localhost -p$pw $db > $db_1_$(date +%d%m%y).sql
done

Import a local SQL File To MySQL on a Remote Server Using SSH Tunnel

I have a connection between my localhost and a remote server using putty SSH tunnel.
Thats fine.
Now I need a command to get the sql file on my local machine i.e. c:\folder\test.sql and import it into mysql on the remote server
I thought maybe...
mysql -u prefix_username -p testpass -h localhost -P 3307 prefix_testdb
then do a command like
mysql -p testpass -u prefix_username prefix_testdb < c:\folder\test.sql
this command did not work.
How can I acheive this?
You should run this command
mysql -h host -u user_name -pPassword database < file.sql > output.log
file.sql contains the sql queries to run and output.log makes sense only when you have a query that returns something (like a select)
The only thing different I can see in your code is the blank space between the -p option and the password. If you use the -p option, you must write the password without leaving any blank space. Or you just can user the option --password=Password
I hope you can solve the problem
You will need to ssh to the remote machine with the mysql command appended:
ssh remote_user#remote_server mysql -p testpass -u username testdb < c:\folder\test.sql
1. mysql -h xxx -uxxx -pxxx . //login to the remote mysql
2. use DATABASE. //assign which db to import
3. source path/to/file.sql //the path can be your local sql file path.
Reference: Import SQL file into mysql
Use 'scp' to copy and mysql to insert to you local machine.
Syntax:
scp remote_user#remove_server:/path/to/sql/file.sql ~/path/to/local/directory
after you transfered the file use:
mysql -uYouUserName -p name_of_database_to_import_to < ~/path/to/local/directory/file.sql
mysql {mydbname} --host {server}.mysql.database.azure.com --user {login} --password={password} < ./{localdbbackupfile}.sql
As managed services, DevOps, and CI/CD workflows have become more popular by this point, most providers of those managed services want to remove the human error part of getting the connection strings correct. If you happen to be using Azure, AWS, GCP, etc, There usually is a page or terminal command that shows you these strings to help you easily integrate. Don't forget to check their docs if you're using something like that. They are auto generated, so they are most likely 'best practice' with spot-on correct syntax for the db version you may be using.
The above command is from "connection strings" on the product details page of my Azure Managed Mysql DB Server instance.
Not necessarily asked, but an fyi, a lot of those services auto generate templates for use in a lot of common connection scenarios:
{
"connectionStrings": {
"ado.net": "Server={server}.mysql.database.azure.com; Port=3306; Database=mytestdb; Uid={login}; Pwd={password};",
"jdbc": "jdbc:mysql://{server}.mysql.database.azure.com:3306/mytestdb?user={login}&password={password}",
"jdbc Spring": "spring.datasource.url=jdbc:mysql://{server}.mysql.database.azure.com:3306/mytestdb spring.datasource.username={login} spring.datasource.password={password}",
"mysql_cmd": "mysql mytestdb --host {server}.mysql.database.azure.com --user {login} --password={password}",
"node.js": "var conn = mysql.createConnection({host: '{server}.mysql.database.azure.com', user: '{login}', password: {password}, database: mytestdb, port: 3306});",
"php": "$con=mysqli_init(); [mysqli_ssl_set($con, NULL, NULL, {ca-cert filename}, NULL, NULL);] mysqli_real_connect($con, '{server}.mysql.database.azure.com', '{login}', '{password}', 'mytestdb', 3306);",
"python": "cnx = mysql.connector.connect(user='{login}', password='{password}', host='{server}.mysql.database.azure.com', port=3306, database='mytestdb')",
"ruby": "client = Mysql2::Client.new(username: '{login}', password: '{password}', database: 'mytestdb', host: '{server}.mysql.database.azure.com', port: 3306)"
}
}
You can use pscp to upload file to the server. Go to your command line and type this
pscp.exe c:\folder\test.sql usernameoftheserver#websitename.com:/serverpath