In my server cluster in pgAdmin I have created 3 different servers. Everytime I create a database through psycopg2, it is being created on all 3 of them. I think this makes sense as when I create the servers they only differ in their name, all the other parameters are the same. host:localhost, port:5432, username:postgres.
Is there a way to create a database to a specific one?
My ini file in psycopg2 looks like this:
[postgresql]
host=localhost
user=postgres
port=5432
password=*
Related
I'm trying to link existing MS Access 2013 tables into a PostgreSQL 12 Database, both 64bit version and installed on a small network using odbc_fdw. The databases are on different machines on a windows network. I have a system DSN installed and checked (using pyodbc).
In PostgreSQL I am able to create the extension; Foreign Data Wrappers; Foreign Server and Foreign Table, and to create the User Mappings, but when trying to run a query I get the "ERROR: Connecting to the driver". I tried numerous options following the little literature I found without any luck. I can use the ocdb_fwd to connect to a MySQL server straight forward but I could not figure out how to do it with MSAccess.
I would deeply appreciate it if somebody can help me figure out how to connect MSAccess to the fdw if possible.
These are my basic settings in PostgreSQL:
CREATE FOREIGN DATA WRAPPER odbc_data_wrapper
HANDLER public.odbc_fdw_handler
VALIDATOR public.odbc_fdw_validator;
CREATE SERVER odbc_msaccess
FOREIGN DATA WRAPPER odbc_data_wrapper
OPTIONS (dsn 'msaccess');
CREATE USER MAPPING FOR postgre SERVER odbc_msaccess
OPTIONS("ocdb_UID" 'Admin', "ocdb_pwd" '');
CREATE FOREIGN TABLE test(
id integer NOT NULL,
name character varying NOT NULL
)
SERVER odbc_msaccess
OPTIONS (layer 'test',
sql_query 'SELECT id, name FROM test);
DSN: msaccess working. Tested on pyodbc
odbc_data_wrapper: tested just fine connecting a MySql database
The databases are on different machines
Yeah, that's likely not going to work.
PostgreSQL needs direct access to the Microsoft Access Database, so it either has to be on the same machine, or on a network share. But if you're running it on a network share, you need to make sure that the user running PostgreSQL has access to the network share, the DSN is installed on the machine running PostgreSQL, and you're properly referring to the network path.
I have a relatively large data table (~4m rows) that has been imported to a locally hosted postgresql database. (As it happens it's a ruby on rails app database, but that shouldn't be important for the purposes of the question - unless it helps)
I want to take that table and add it into an identical table in a heroku postgresql database (the table is currently empty).
How would I do that quickly and efficiently?
I found this Copy a table from one database to another in Postgres
but I'm struggling with the syntax for the heroku end, i.e. how do I connect to both at the same time? Which database am I connecting to originally?
In that answer, you are originally connected to the database "source_db" or "my_db" (depending on which line in the answer you are looking at). Presumably that database is on the instance running locally on port 5432, unless unshown environment variables (or non-default compilation) have changed that. And the destination database is named "target_db", running in the same instance.
The pg_dump and psql are independent commands and each takes all the connection options that they would take if run in isolation. So you would probably want something like:
pg_dump -t table_to_copy source_db | psql target_db -h you.heroku.hostname_or_ip
A problem could be if both commands prompt for a password, it might make a mess. Which password do you need to enter first? And whichever order, will they read them correctly? If both need passwords, it is best to arrange that at least one of them be supplied by ~/.pgpass.
I have PostgreSQL 9.3 version. I have created database name db1 now I need it to share with other users who all are connected with the LAN's to connect other applications with the same database.
In SQL Server: We can do this by selecting server name with login details.
Question:
Is it possible in PostgreSQL?
If yes, how can do this?
What is the procedure?
You will need to modify pg_hba.conf to allow remote connections to the database. Information about pg_hba.conf can be found here.
After that, you can connect programatically with a connection string, or similar to your image, with a GUI application like pgAdmin.
To connect (remotely or locally) from pgAdmin choose File -> Add Server... and enter the connection information into the dialog box. Here's an example of the window:
Your client computers will also need to have PostgreSQL drivers as well. If you're doing this in Windows, you'll probably be using ODBC. The PostgreSQL ODBC drivers are here. Info on the connection string format can be found here.
Here's an example of what pgAdmin looks like:
I'm not sure if used the right terminology in my question but here's what I'm trying to do. From the command line, I am used to running the following command :
psql -U postgres
and then I can see a list of all my databases by doing:
postgres=# \l
I'm wondering how to do the same thing programmatically in lua?
The following function is what I currently use to connect to a specific db:
local db_env, db_con
local connect_db = function()
if not con then
db_env = assert (luasql.postgres())
db_con = assert (db_env:connect(databasename, databaseUser, databasepassword))
end
end
Just wondering how I would change it to connect to the instance of postgresql server to see all the dbs that are hosted by my server.
Thanks.
Edit 1
Perhaps when I'm running the command
psql -U postgres
it is connecting to a default database?
In your code you have to connect to the database server and query the server for a list of databases. The database will return a recordset containing a list of the databases on that server.
A great walk-through of connecting to postgres and executing queries ( like the one above that hjpotter92 posted).
Hitting Postgres From Lua
Hope you find it helpful
I have two instances on two separate AIX servers. Both DB2 databases are up with their sample databases. I want to connect with one DB2 instance from the other instance.
I am new on DB2 and want to know how can I do that.
I have executed following commands:
This path is client instance:
#. /home/db2inst2/sqllib/db2profile
db2 catalog tcpip node db2serverinst remote lpar1 server 50005 remote_instance ctginst1
db2 => terminate
DB20000I The TERMINATE command completed successfully.
# db2 connect to db2serverinst user ctginst1
Enter current password for ctginst1: SQL1031N The database directory cannot be found on the indicated file system.
SQLSTATE=58031
The term "Connect to an instance" is not correct. The correct one is to attach:
Machine A
db2inst1
db-1
db-2
sample
Machine B
db2inst2
db-3
db-4
sample
db2inst3
sample
If you want to execute commands from Machine A into Machine B, you have to catalog db2inst2 AND/OR db2inst3 in db2inst1 (catalog tcpip node). Remember that you have to give different names to each cataloged object.
Once you have cataloged a remote instance, you could also issue database commands, but you have to catalog remote databases. You can have a remote sample, and a local sample, but alias should be different.
If you cataloged db2inst2 instance as db2inst2 node in local node from Machine A.
And you cataloged sample database as sample-2 alias in local node from Machine A.
You could execute in Machine A.
db2 attach to db2inst2
db2 connect to sample-2
Attach is for 'administrative' things, like list applciations, create db, etc.
Connect is to use the database.
You have only cataloged DB2 instance. You need to catalog the database at the remote server:
db2 catalog database XXXX at node db2serverinst
Obviously, you'll need to replace the XXXX with the actual name of the remote database. Once you've cataloged the database, you can then connect:
db2 connect to XXXX user ctginst1
Note, this only defines the connection. You won't be able to access objects within this remote database from other databases within the local instance without configuring federation.